using AsbCloudApp.Data; using AsbCloudApp.Data.SAUB; using AsbCloudApp.Data.Subsystems; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudApp.Services.Subsystems; using AsbCloudDb.Model; using AsbCloudDb.Model.Subsystems; using AsbCloudInfrastructure.Background; using AsbCloudInfrastructure.Repository; using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services.DailyReport; using AsbCloudInfrastructure.Services.DetectOperations; using AsbCloudInfrastructure.Services.DrillingProgram; using AsbCloudInfrastructure.Services.ProcessMap; using AsbCloudInfrastructure.Services.SAUB; using AsbCloudInfrastructure.Services.Subsystems; using AsbCloudInfrastructure.Services.Trajectory; using AsbCloudInfrastructure.Services.WellOperationService; using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; using AsbCloudApp.Data.Manuals; using AsbCloudApp.Services.AutoGeneratedDailyReports; using AsbCloudApp.Services.Notifications; using AsbCloudApp.Services.WellOperationImport; using AsbCloudDb.Model.Manuals; using AsbCloudInfrastructure.Services.AutoGeneratedDailyReports; using AsbCloudInfrastructure.Services.WellOperationImport; using AsbCloudInfrastructure.Services.WellOperationImport.FileParser; using AsbCloudInfrastructure.Services.ProcessMap.ProcessMapWellboreDevelopment; using AsbCloudApp.Data.WellOperationImport.Options; namespace AsbCloudInfrastructure { public static class DependencyInjection { public static IAsbCloudDbContext MakeContext(string connectionString) { var options = new DbContextOptionsBuilder() .UseNpgsql(connectionString) .Options; var context = new AsbCloudDbContext(options); return context; } public static void MapsterSetup() { TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .MapWith((source) => source.DateTime); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .MapWith((source) => source == default ? new DateTime(0, DateTimeKind.Utc) : source); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .MapWith((source) => source.MakeTimeOnly()); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .MapWith((source) => new(source)); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .MapWith((source) => new(source)); #pragma warning disable CS8603 // Possible null reference return. TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .Ignore(dst => dst.Cluster, dst => dst.RelationCompaniesWells, dst => dst.Telemetry, dst => dst.WellComposites, dst => dst.WellCompositeSrcs, dst => dst.WellOperations, dst => dst.WellType); #pragma warning restore CS8603 // Possible null reference return. TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .Ignore(dst => dst.Deposit, dst => dst.Wells); TypeAdapterConfig.GlobalSettings.Default.Config .ForType(); TypeAdapterConfig.GlobalSettings.Default.Config .ForType(); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .Ignore(dst => dst.NotificationCategory, dst => dst.User); } public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { MapsterSetup(); var connectionStringName = "DefaultConnection"; #if DEBUG connectionStringName = "DebugConnection"; #endif services.AddDbContext(options => options.UseNpgsql(configuration.GetConnectionString(connectionStringName))); services.AddMemoryCache(); services.AddScoped(provider => provider.GetRequiredService()); services.AddSingleton(new WitsInfoService()); services.AddSingleton(provider => TelemetryDataCache.GetInstance(provider)); services.AddSingleton(provider => TelemetryDataCache.GetInstance(provider)); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(provider => ReduceSamplingService.GetInstance(configuration)); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient, CrudWellRelatedRepositoryBase>(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient, CrudCacheRepositoryBase>(); // admin crud services: services.AddTransient, CrudCacheRepositoryBase>(s => new CrudCacheRepositoryBase( s.GetRequiredService(), s.GetRequiredService(), dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService services.AddTransient, CrudCacheRepositoryBase>(s => new CrudCacheRepositoryBase( s.GetRequiredService(), s.GetRequiredService(), dbSet => dbSet.Include(d => d.Clusters))); services.AddTransient, CrudCacheRepositoryBase>(s => new CrudCacheRepositoryBase( s.GetRequiredService(), s.GetRequiredService(), dbSet => dbSet.Include(c => c.CompanyType))); services.AddTransient, CrudCacheRepositoryBase>(); services.AddTransient, CrudCacheRepositoryBase>(s => new CrudCacheRepositoryBase( s.GetRequiredService(), s.GetRequiredService(), dbSet => dbSet .Include(c => c.Wells) .Include(c => c.Deposit))); // может быть включен в сервис ClusterService services.AddTransient, CrudCacheRepositoryBase>(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient, CrudCacheRepositoryBase>(); // Subsystem service services.AddTransient, CrudCacheRepositoryBase>(); services.AddTransient(); services.AddTransient, CrudCacheRepositoryBase>(); // TelemetryData services services.AddTransient(); services.AddTransient, TelemetryDataSpinService>(); // Wits services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient, CrudRepositoryBase>(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient, WellOperationDefaultExcelParser>(); services.AddTransient, WellOperationGazpromKhantosExcelParser>(); return services; } public static IServiceCollection AddTransientLazy(this IServiceCollection services) where TService : class where TImplementation : class, TService => services.AddTransient() .AddTransient(provider => new Lazy(provider.GetRequiredService)); public static IServiceCollection AddTransientLazy(this IServiceCollection services, Func implementationFactory) where TService : class where TImplementation : class, TService => services.AddTransient(implementationFactory) .AddTransient(provider => new Lazy(() => implementationFactory(provider))); } }