Simplify install and organized your ServiceCollection items with installer approach.
dotnet add package ServiceCollectionInstaller
public class InfrastructureInstaller : IServiceCollectionInstaller
{
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlServer(configuration["defult-connection"]);
});
// other configuration
}
}
// IAssemblyMaker is a simple interface that refer to assembly, you can use another type
using assemblyApplication = Installer.ConsumerWebAppl.IAssemblyMaker;
using assemblyInfrastructure = Installer.ConsumerWebApp.Infrastructure.IAssemblyMaker;
var builder = WebApplication.CreateBuilder(args);
{
// get Configuration
var config = builder.Configuration;
// Install from your assemblies
builder.Services.InstallFromAssembly<assemblyApplication>(config);
builder.Services.InstallFromAssembly<assemblyInfrastructure>(config);
}
var app = builder.Build();
app.Run();
// IAssemblyMaker is a simple interface that refer to assembly, you can use another type
using assemblyApplication = Installer.ConsumerWebAppl.IAssemblyMaker;
using assemblyInfrastructure = Installer.ConsumerWebApp.Infrastructure.IAssemblyMaker;
var builder = WebApplication.CreateBuilder(args);
{
// get Configuration
var config = builder.Configuration;
// Install from your assemblies - 1
builder.Services.Installer<assemblyApplication>(config).Finish();
// Install from your assemblies - 2
builder.Services.Installer<assemblyApplication>(config)
.NextOne<assemblyInfrastructure>()
//.NextOne<T>() ...
.Finish();
// Install from your assemblies - 2
builder.Services.Installer<assemblyApplication>(config)
.Finish<assemblyInfrastructure>();
}
var app = builder.Build();
app.Run();