forked from ddrilling/AsbCloudServer
129 lines
7.4 KiB
C#
129 lines
7.4 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Data.SAUB;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Services;
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
using AsbCloudInfrastructure.Services.DetectOperations;
|
|
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<AsbCloudDbContext>()
|
|
.UseNpgsql(connectionString)
|
|
.Options;
|
|
var context = new AsbCloudDbContext(options);
|
|
return context;
|
|
}
|
|
|
|
public static void MapsterSetup()
|
|
{
|
|
TypeAdapterConfig.GlobalSettings.Default.Config
|
|
.ForType<DateTimeOffset, DateTime>()
|
|
.MapWith((source) => source.DateTime);
|
|
|
|
TypeAdapterConfig.GlobalSettings.Default.Config
|
|
.ForType<DateTime, DateTimeOffset>()
|
|
.MapWith((source) => source == default ? new DateTime(0, DateTimeKind.Utc) : source);
|
|
|
|
}
|
|
|
|
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
MapsterSetup();
|
|
services.AddDbContext<AsbCloudDbContext>(options =>
|
|
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
services.AddFluentValidation();
|
|
|
|
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>());
|
|
services.AddScoped<IFileShareService, GoogleDriveService>();
|
|
services.AddScoped<IEmailService, EmailService>();
|
|
|
|
services.AddHostedService<OperationDetectionBackgroundService>();
|
|
|
|
services.AddSingleton(new WitsInfoService());
|
|
services.AddSingleton(new CacheDb());
|
|
services.AddSingleton(new InstantDataRepository());
|
|
services.AddSingleton<ITelemetryTracker, TelemetryTracker>();
|
|
services.AddSingleton<IRequerstTrackerService, RequestTrackerService>();
|
|
services.AddSingleton<IBackgroundWorkerService, BackgroundWorkerService>();
|
|
|
|
services.AddTransient<IAuthService, AuthService>();
|
|
services.AddTransient<IClusterService, ClusterService>();
|
|
services.AddTransient<IDrillFlowChartService, DrillFlowChartService>();
|
|
services.AddTransient<IDrillingProgramService, DrillingProgramService>();
|
|
services.AddTransient<IDrillParamsService, DrillParamsService>();
|
|
services.AddTransient<IEventService, EventService>();
|
|
services.AddTransient<IFileService, FileService>();
|
|
services.AddTransient<IMeasureService, MeasureService>();
|
|
services.AddTransient<IMessageService, MessageService>();
|
|
services.AddTransient<IOperationsStatService, OperationsStatService>();
|
|
services.AddTransient<IReportService, ReportService>();
|
|
services.AddTransient<ISetpointsService, SetpointsService>();
|
|
services.AddTransient<ITelemetryService, TelemetryService>();
|
|
services.AddTransient<ITelemetryUserService, TelemetryUserService>();
|
|
services.AddTransient<ITimezoneService, TimezoneService>();
|
|
services.AddTransient<IUserService, UserService>();
|
|
services.AddTransient<IUserRoleService, UserRoleService>();
|
|
services.AddTransient<IWellService, WellService>();
|
|
services.AddTransient<IWellCompositeService, WellCompositeService>();
|
|
services.AddTransient<IWellOperationImportService, WellOperationImportService>();
|
|
services.AddTransient<IWellOperationService, WellOperationService>();
|
|
services.AddTransient<IScheduleReportService, ScheduleReportService>();
|
|
services.AddTransient<IDetectedOperationService, DetectedOperationService>();
|
|
|
|
// admin crud services:
|
|
services.AddTransient<ICrudService<TelemetryDto>, CrudServiceBase<TelemetryDto, Telemetry>>(); // может быть включен в сервис TelemetryService
|
|
services.AddTransient<ICrudService<DrillParamsDto>, DrillParamsService>();
|
|
services.AddTransient<ICrudService<DepositDto>, CrudCacheServiceBase<DepositDto, Deposit>>();
|
|
services.AddTransient<ICrudService<CompanyDto>, CrudCacheServiceBase<CompanyDto, Company>>();
|
|
services.AddTransient<ICrudService<CompanyTypeDto>, CrudCacheServiceBase<CompanyTypeDto, CompanyType>>();
|
|
services.AddTransient<ICrudService<ClusterDto>, CrudCacheServiceBase<ClusterDto, Cluster>>(); // может быть включен в сервис ClusterService
|
|
services.AddTransient<ICrudService<PermissionDto>, CrudCacheServiceBase<PermissionDto, Permission>>();
|
|
|
|
// TelemetryData services
|
|
services.AddTransient<ITelemetryDataService<TelemetryDataSaubDto>, TelemetryDataSaubService>();
|
|
services.AddTransient<ITelemetryDataService<TelemetryDataSpinDto>, TelemetryDataSpinService>();
|
|
|
|
// Wits
|
|
services.AddTransient<IWitsRecordRepository<AsbCloudApp.Data.WITS.Record1Dto>, WitsRecordRepository<AsbCloudApp.Data.WITS.Record1Dto, AsbCloudDb.Model.WITS.Record1>>();
|
|
services.AddTransient<IWitsRecordRepository<AsbCloudApp.Data.WITS.Record7Dto>, WitsRecordRepository<AsbCloudApp.Data.WITS.Record7Dto, AsbCloudDb.Model.WITS.Record7>>();
|
|
services.AddTransient<IWitsRecordRepository<AsbCloudApp.Data.WITS.Record8Dto>, WitsRecordRepository<AsbCloudApp.Data.WITS.Record8Dto, AsbCloudDb.Model.WITS.Record8>>();
|
|
services.AddTransient<IWitsRecordRepository<AsbCloudApp.Data.WITS.Record50Dto>, WitsRecordRepository<AsbCloudApp.Data.WITS.Record50Dto, AsbCloudDb.Model.WITS.Record50>>();
|
|
services.AddTransient<IWitsRecordRepository<AsbCloudApp.Data.WITS.Record60Dto>, WitsRecordRepository<AsbCloudApp.Data.WITS.Record60Dto, AsbCloudDb.Model.WITS.Record60>>();
|
|
services.AddTransient<IWitsRecordRepository<AsbCloudApp.Data.WITS.Record61Dto>, WitsRecordRepository<AsbCloudApp.Data.WITS.Record61Dto, AsbCloudDb.Model.WITS.Record61>>();
|
|
|
|
services.AddValidators();
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddTransientLazy<TService, TImplementation>(this IServiceCollection services)
|
|
where TService : class
|
|
where TImplementation : class, TService
|
|
=> services.AddTransient<TService, TImplementation>()
|
|
.AddTransient(provider => new Lazy<TService>(provider.GetService<TService>));
|
|
|
|
public static IServiceCollection AddTransientLazy<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory)
|
|
where TService : class
|
|
where TImplementation : class, TService
|
|
=> services.AddTransient<TService, TImplementation>(implementationFactory)
|
|
.AddTransient(provider => new Lazy<TService>(() => implementationFactory(provider)));
|
|
|
|
}
|
|
}
|