first commit
This commit is contained in:
65
MyNewProjectName.Infrastructure/DependencyInjection.cs
Normal file
65
MyNewProjectName.Infrastructure/DependencyInjection.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MyNewProjectName.Application.Interfaces;
|
||||
using MyNewProjectName.Domain.Interfaces;
|
||||
using MyNewProjectName.Infrastructure.Options;
|
||||
using MyNewProjectName.Infrastructure.Persistence.Context;
|
||||
using MyNewProjectName.Infrastructure.Persistence.Repositories;
|
||||
using MyNewProjectName.Infrastructure.Services;
|
||||
|
||||
namespace MyNewProjectName.Infrastructure;
|
||||
|
||||
/// <summary>
|
||||
/// Dependency Injection for Infrastructure Layer
|
||||
/// </summary>
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
// Register Options Pattern
|
||||
services.Configure<DatabaseOptions>(configuration.GetSection(DatabaseOptions.SectionName));
|
||||
services.Configure<JwtOptions>(configuration.GetSection(JwtOptions.SectionName));
|
||||
services.Configure<RedisOptions>(configuration.GetSection(RedisOptions.SectionName));
|
||||
services.Configure<SerilogOptions>(configuration.GetSection(SerilogOptions.SectionName));
|
||||
|
||||
// Validate required Options on startup
|
||||
services.AddOptions<DatabaseOptions>()
|
||||
.Bind(configuration.GetSection(DatabaseOptions.SectionName))
|
||||
.ValidateDataAnnotations()
|
||||
.ValidateOnStart();
|
||||
|
||||
// Validate JwtOptions only if section exists (optional for now)
|
||||
var jwtSection = configuration.GetSection(JwtOptions.SectionName);
|
||||
if (jwtSection.Exists() && jwtSection.GetChildren().Any())
|
||||
{
|
||||
services.AddOptions<JwtOptions>()
|
||||
.Bind(jwtSection)
|
||||
.ValidateDataAnnotations()
|
||||
.ValidateOnStart();
|
||||
}
|
||||
|
||||
// Register DbContext using Options Pattern
|
||||
services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
var dbOptions = serviceProvider.GetRequiredService<IOptions<DatabaseOptions>>().Value;
|
||||
options.UseSqlServer(
|
||||
dbOptions.DefaultConnection,
|
||||
b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
|
||||
});
|
||||
|
||||
// Register repositories
|
||||
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
|
||||
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
||||
|
||||
// Register HttpContextAccessor (required for CurrentUserService)
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
// Register services
|
||||
services.AddScoped<IDateTimeService, DateTimeService>();
|
||||
services.AddScoped<ICurrentUserService, CurrentUserService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user