更新时间:2023-02-16 14:46:35
对于遵循SOLID原则的任何相当大尺寸应用程序的产品开发,vNext的内置DI容器将无用,因为:
内置容器还有其他限制,例如只能使用单个构造函数支持类型,但这是非常合理的默认值,因为您的注射器应该只有一个构造函数。自从rc1以来,现在有了一种多功能的支持。
如果你从一个新的和简单的项目开始,我的建议是应用纯DI (这意味着手机组件没有使用容器)并通过插入您的自定义IControllerActivator 。之后,当批处理注册和装饰等功能将提高组成根目录的维护性,切换到符合您要求的已建立的DI库之一。
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);
}
For product development of any considerably sized application that follows the SOLID principles, vNext's built-in DI container will be useless, because:
There are other limitations to the built-in container, such as only being able to support types with a single constructor, but this is a very reasonable default, because your injectables should only have a single constructor anyway. since rc1, there is now some kind of multi-ctor support.
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.