DotNet Core 是否仍然需要 Unity 依赖注入?
Does DotNet Core still require Unity Dependency Injection?
当我完成一个简单的 DotNet Core Web 应用程序设置时。看起来不需要 Unity 依赖注入,因为你可以简单地在 Startup.cs ConfigureServices 方法中完成。
是的,你可以使用框架提供的依赖注入服务。
The ConfigureServices method in the Startup class is responsible for defining the services the application will use, including platform features like Entity Framework Core and ASP.NET Core MVC. Initially, the IServiceCollection provided to ConfigureServices has just a handful of services defined. Below is an example of how to add additional services to the container using a number of extension methods like AddDbContext, AddIdentity, and AddMvc.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
依赖注入是 dotnet 核心框架中的第一个 class 公民。所以不需要单独下载Unity、Autofac、Ninject等包
当我完成一个简单的 DotNet Core Web 应用程序设置时。看起来不需要 Unity 依赖注入,因为你可以简单地在 Startup.cs ConfigureServices 方法中完成。
是的,你可以使用框架提供的依赖注入服务。
The ConfigureServices method in the Startup class is responsible for defining the services the application will use, including platform features like Entity Framework Core and ASP.NET Core MVC. Initially, the IServiceCollection provided to ConfigureServices has just a handful of services defined. Below is an example of how to add additional services to the container using a number of extension methods like AddDbContext, AddIdentity, and AddMvc.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
依赖注入是 dotnet 核心框架中的第一个 class 公民。所以不需要单独下载Unity、Autofac、Ninject等包