学海无涯

导航

集中添加依赖注入DI

基础设施项目中集中添加依赖注入类:

namespace CleanArchitectureApp.Infrastructure;
public static class DependencyInjection
{
  public static IServiceCollection AddInfrastructureServices(this IServiceCollection services,
    IConfiguration configuration)
  {
    services.AddDbContext<AppDbContext>(options =>
    {
      options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
    });
    return services;
  }
}

启动项目 Program.cs 文件中注册:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddInfrastructureServices(builder.Configuration);

  

posted on 2023-10-02 10:53  宁静致远.  阅读(23)  评论(0编辑  收藏  举报