As currently there is lack of documentation on DI topic - Dependency Injection. Can someone help me to understand following:
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); }
- What are pros/cons of using built-in DI over existing solutions like (NInject, Autofac, Structure Map)?
- What are current limitations of default dependency injection (if any)?
To answer your first question: it seems that ASP.NET docs were updated and now clearly state what is each type of registration for:
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.
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/
For product development of any considerably sized application that follows the SOLID principles, vNext's built-in DI container will be useless, because:
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.
Here it is explained :
Alpha version had this limitations :
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.