Why would one use a third party DI Container over

2019-01-03 02:53发布

As currently there is lack of documentation on DI topic - Dependency Injection. Can someone help me to understand following:

  1. What is the difference between these registrations?

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<IService, Service>();
        services.AddScoped<IService, Service>();
        services.AddSingleton<IService, Service>();
        services.AddInstance(service);
    }
    
  2. What are pros/cons of using built-in DI over existing solutions like (NInject, Autofac, Structure Map)?
  3. What are current limitations of default dependency injection (if any)?

4条回答
Animai°情兽
2楼-- · 2019-01-03 03:38

To answer your first question: it seems that ASP.NET docs were updated and now clearly state what is each type of registration for:

ASP.NET services can be configured with the following lifetimes:

Transient

Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless service.

Scoped

Scoped lifetime services are created once per request.

Singleton

Singleton lifetime services are created the first time they are requested, and then every subsequent request will use the same instance. If your application requires singleton behavior, allowing the services container to manage the service’s lifetime is recommended instead of implementing the singleton design pattern and managing your object’s lifetime in the class yourself.

Instance [pre RTM only!]

You can choose to add an instance directly to the services container. If you do so, this instance will be used for all subsequent requests (this technique will create a Singleton-scoped instance). One key difference between Instance services and Singleton services is that the Instance service is created in ConfigureServices, while the Singleton service is lazy-loaded the first time it is requested.


Updated in RTM

Note that in Asp.Net Core RTM docs Instance was removed. Instance is basically the same thing as Singleton, but they had different initialization semantics (Singleton was lazy loaded). But now there is no AddInstance API, only AddSignleton which can take already created instance.

查看更多
Viruses.
3楼-- · 2019-01-03 03:45

What is the difference between these registrations?

  • Transient - instantiated every time it is retrieved
  • Scoped - instantiated once per http request and will be available for the lifetime of the http request
  • Singleton - instantiated once and will be available for the entire lifetime of your application
  • Instance - equivalent to singleton except you provide the object instance instead of the framework creating the instance

Source: http://www.khalidabuhakmeh.com/asp-vnext-dependency-injection-lifecycles, http://dotnetliberty.com/index.php/2015/10/15/asp-net-5-mvc6-dependency-injection-in-6-steps/

查看更多
爷、活的狠高调
4楼-- · 2019-01-03 03:48

For product development of any considerably sized application that follows the SOLID principles, vNext's built-in DI container will be useless, because:

  • It doesn't help you in verifying your configuration, making it really hard to diagnose problems that come from common misconfigurations. In a reasonably sized application, it's actually quite hard to spot these mistakes yourself.
  • It is impossible to apply cross-cutting concerns using interceptors or decorators in a maintainable fashion. This makes maintaining any reasonably sized application really expensive.
  • Although it supports mapping of open generic abstractions to open generic implementations, its implementation is rather naive and is unable to work with generic types with type constraints, and more complex generic type mappings.
  • It is impossible to make conditional registrations, in such way that registrations only get injected to a certain set of consumers.

If you start with a new and simple project, my advice is to apply Pure DI (which means hand-wired components without the use of a container) and resolve your types by plugging in your custom IControllerActivator. Later on, when features such as batch-registration and decoration would improve maintainability of your Composition Root, switch to one of the established DI libraries that fits your requirements.

查看更多
forever°为你锁心
5楼-- · 2019-01-03 03:49

Here it is explained :

  • Transient - A new instance is created every time
  • Scoped - A single instance is created inside the current scope. It is equivalent to Singleton in the current scope
  • Singleton - A single instance is created and it acts like a singleton
  • Instance - A specific instance is given all the time. You are responsible for its initial creation

Alpha version had this limitations :

  • It only supports constructor injection
  • It can only resolve types with one and only one public constructor
  • It doesn’t support advanced features (like per thread scope or auto discovery)

If you aren't writing really complicated product default DI container should be sufficient for you. In other cases you can try libraries you already mentioned that have advanced features.

My advice would be to start with the default one and change implementation when(if) you hit something you can't do with it.

查看更多
登录 后发表回答