using AsbCloudApp.Data; using AsbCloudApp.Data.SAUB; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services.Analysis; using AsbCloudInfrastructure.Services.Cache; using AsbCloudInfrastructure.Services.DailyReport; using AsbCloudInfrastructure.Services.DrillingProgram; using AsbCloudInfrastructure.Services.SAUB; using AsbCloudInfrastructure.Services.WellOperationService; using AsbCloudInfrastructure.Validators; 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); } public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { MapsterSetup(); services.AddDbContext(options => options.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))); services.AddFluentValidation(); services.AddScoped(provider => provider.GetService()); services.AddScoped(); services.AddScoped(); services.AddHostedService();// replace by BackgroundWorkerService services.AddSingleton(new WitsInfoService()); services.AddSingleton(new CacheDb()); services.AddSingleton(new InstantDataRepository()); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); 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(); // admin crud services: services.AddTransient, CrudServiceBase>(); // может быть включен в сервис TelemetryService services.AddTransient, DrillParamsService>(); services.AddTransient, CrudCacheServiceBase>(); services.AddTransient, CrudCacheServiceBase>(); services.AddTransient, CrudCacheServiceBase>(); services.AddTransient, CrudCacheServiceBase>(); // может быть включен в сервис ClusterService 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))); } }