using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.Reflection; using ZeroFramework.DeviceCenter.Domain.Aggregates.BuyerAggregate; using ZeroFramework.DeviceCenter.Domain.Aggregates.DeviceAggregate; using ZeroFramework.DeviceCenter.Domain.Aggregates.MeasurementAggregate; using ZeroFramework.DeviceCenter.Domain.Aggregates.OrderAggregate; using ZeroFramework.DeviceCenter.Domain.Aggregates.PermissionAggregate; using ZeroFramework.DeviceCenter.Domain.Repositories; using ZeroFramework.DeviceCenter.Infrastructure.ConnectionStrings; using ZeroFramework.DeviceCenter.Infrastructure.Constants; using ZeroFramework.DeviceCenter.Infrastructure.EntityFrameworks; using ZeroFramework.DeviceCenter.Infrastructure.Repositories; namespace ZeroFramework.DeviceCenter.Infrastructure { public static class DependencyRegistrar { public static IServiceCollection AddInfrastructureLayer(this IServiceCollection services, IConfiguration configuration) { services.AddTransient(); services.AddEntityFrameworkSqlServer(); bool isDemoMode = Convert.ToBoolean(configuration.GetRequiredSection("UseDemoLaunchMode").Value); services.AddDbContext((serviceProvider, optionsBuilder) => { optionsBuilder.UseSqlServer(configuration.GetConnectionString(DbConstants.DefaultConnectionStringName), sqlOptions => { sqlOptions.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name); sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); var connectionStringProvider = serviceProvider.GetRequiredService(); optionsBuilder.AddInterceptors(new TenantDbConnectionInterceptor(connectionStringProvider)); if (isDemoMode) { optionsBuilder.AddInterceptors(new DisalbeModifiedDeletedSaveChangesInterceptor()); } IMediator mediator = serviceProvider.GetService() ?? new NullMediator(); optionsBuilder.AddInterceptors(new CustomSaveChangesInterceptor(mediator)); optionsBuilder.AddInterceptors(new CustomDbCommandInterceptor()); optionsBuilder.UseInternalServiceProvider(serviceProvider); }, ServiceLifetime.Transient, ServiceLifetime.Singleton); services.AddPooledDbContextFactory((serviceProvider, optionsBuilder) => { optionsBuilder.UseSqlServer(configuration.GetConnectionString(DbConstants.DefaultConnectionStringName), sqlOptions => { sqlOptions.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name); sqlOptions.EnableRetryOnFailure(maxRetryCount: 10, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null); }); var connectionStringProvider = serviceProvider.GetRequiredService(); optionsBuilder.AddInterceptors(new TenantDbConnectionInterceptor(connectionStringProvider)); if (isDemoMode) { optionsBuilder.AddInterceptors(new DisalbeModifiedDeletedSaveChangesInterceptor()); } IMediator mediator = serviceProvider.GetService() ?? new NullMediator(); optionsBuilder.AddInterceptors(new CustomSaveChangesInterceptor(mediator)); optionsBuilder.AddInterceptors(new CustomDbCommandInterceptor()); optionsBuilder.UseInternalServiceProvider(serviceProvider); }); services.AddTransient(typeof(IRepository<>), typeof(DeviceCenterEfCoreRepository<>)); services.AddTransient(typeof(IRepository<,>), typeof(DeviceCenterEfCoreRepository<,>)); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.Configure(options => { options.ConfigIncludes(e => e.Include(e => e.Devices)); }); return services; } } }