集中添加依赖注入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);