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.Repository; using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services.Cache; using AsbCloudInfrastructure.Services.DailyReport; using AsbCloudInfrastructure.Services.DetectOperations; using AsbCloudInfrastructure.Services.DrillingProgram; using AsbCloudInfrastructure.Services.SAUB; using AsbCloudInfrastructure.Services.Subsystems; using AsbCloudInfrastructure.Services.WellOperationService; using AsbCloudInfrastructure.Validators; using DocumentFormat.OpenXml.Spreadsheet; using FluentValidation.AspNetCore; using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; 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 == default ? default : source.MakeTimeOnly()); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .MapWith((source) => new(source)); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .MapWith((source) => new(source)); 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); TypeAdapterConfig.GlobalSettings.Default.Config .ForType() .Ignore(dst => dst.Deposit, dst => dst.Wells); TypeAdapterConfig.GlobalSettings.Default.Config .ForType(); TypeAdapterConfig.GlobalSettings.Default.Config .ForType(); } 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.AddFluentValidation(); services.AddScoped(provider => provider.GetService()); services.AddScoped(); services.AddHostedService(); services.AddHostedService(); services.AddSingleton(new WitsInfoService()); services.AddSingleton(new CacheDb()); services.AddSingleton(new InstantDataRepository()); services.AddSingleton(provider=> TelemetryDataCache.GetInstance(configuration)); services.AddSingleton(provider=> TelemetryDataCache.GetInstance(configuration)); 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, CrudWellRelatedServiceBase>(); services.AddTransient(); services.AddTransient(); services.AddTransient(); // admin crud services: services.AddTransient, CrudServiceBase>(s => new CrudCacheServiceBase( s.GetService(), dbSet => dbSet.Include(t => t.Well))); // может быть включен в сервис TelemetryService services.AddTransient, DrillParamsService>(); services.AddTransient, CrudCacheServiceBase>(s => new CrudCacheServiceBase( s.GetService(), dbSet => dbSet.Include(d => d.Clusters))); services.AddTransient, CrudCacheServiceBase>(s => new CrudCacheServiceBase( s.GetService(), dbSet => dbSet.Include(c => c.CompanyType))); services.AddTransient, CrudCacheServiceBase>(); services.AddTransient, CrudCacheServiceBase>(s => new CrudCacheServiceBase( s.GetService(), dbSet => dbSet .Include(c => c.Wells) .Include(c => c.Deposit))); // может быть включен в сервис ClusterService services.AddTransient, CrudCacheServiceBase>(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); // Subsystem service services.AddTransient, CrudCacheServiceBase>(); services.AddTransient(); services.AddTransient, CrudCacheServiceBase>(); // TelemetryData services services.AddTransient, TelemetryDataSaubService>(); services.AddTransient, TelemetryDataSpinService>(); // Wits services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddTransient, WitsRecordRepository>(); services.AddValidators(); return services; } public static IServiceCollection AddTransientLazy(this IServiceCollection services) where TService : class where TImplementation : class, TService => services.AddTransient() .AddTransient(provider => new Lazy(provider.GetService)); 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))); } }