DD.WellWorkover.Cloud/AsbCloudInfrastructure/DependencyInjection.cs

118 lines
6.2 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
2021-10-01 15:44:56 +05:00
using AsbCloudInfrastructure.Services.Analysis;
2021-04-07 18:01:56 +05:00
using AsbCloudInfrastructure.Services.Cache;
using AsbCloudInfrastructure.Services.WellOperationService;
2022-01-10 16:00:09 +05:00
using AsbCloudInfrastructure.Validators;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
2021-12-01 11:49:59 +05:00
using System;
using FluentValidation;
using FluentValidation.AspNetCore;
using AsbCloudInfrastructure.Services.DrillingProgram;
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;
}
2022-01-09 11:46:27 +05:00
public static void MapsterSetup()
{
TypeAdapterConfig.GlobalSettings.Default.Config
.ForType<DateTimeOffset, DateTime>()
.MapWith((source) => source.DateTime);
TypeAdapterConfig.GlobalSettings.Default.Config
.ForType<DateTime, DateTimeOffset>()
2022-01-09 11:46:27 +05:00
.MapWith((source) => source == default ? new DateTime(0, DateTimeKind.Utc) : source);
}
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
2022-01-09 11:46:27 +05:00
MapsterSetup();
services.AddDbContext<AsbCloudDbContext>(options =>
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")));
services.AddFluentValidation();
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>());
services.AddScoped<IFileShareService, GoogleDriveService>();
2022-02-28 14:44:26 +05:00
services.AddScoped<IEmailService, EmailService>();
services.AddHostedService<TelemetryAnalyticsBackgroundService>();// replace by BackgroundWorkerService
2021-04-07 18:01:56 +05:00
services.AddSingleton(new CacheDb());
services.AddSingleton<ITelemetryTracker, TelemetryTracker>();
2022-02-08 13:03:56 +05:00
services.AddSingleton<IRequerstTrackerService, RequestTrackerService>();
services.AddSingleton<IBackgroundWorkerService, BackgroundWorkerService>();
services.AddTransient<IAuthService, AuthService>();
services.AddTransient<IClusterService, ClusterService>();
2021-12-03 09:44:10 +05:00
services.AddTransient<IDrillFlowChartService, DrillFlowChartService>();
services.AddTransient<IDrillingProgramService, DrillingProgramService>();
services.AddTransient<IDrillParamsService, DrillParamsService>();
2021-04-23 10:21:25 +05:00
services.AddTransient<IEventService, EventService>();
2021-12-03 09:44:10 +05:00
services.AddTransient<IFileService, FileService>();
services.AddTransient<IMeasureService, MeasureService>();
services.AddTransient<IMessageService, MessageService>();
2021-12-20 15:17:09 +05:00
services.AddTransient<IOperationsStatService, OperationsStatService>();
services.AddTransient<IReportService, ReportService>();
services.AddTransient<ISetpointsService, SetpointsService>();
services.AddTransient<ITelemetryAnalyticsService, TelemetryAnalyticsService>();
2021-12-03 09:44:10 +05:00
services.AddTransient<ITelemetryService, TelemetryService>();
services.AddTransient<ITelemetryUserService, TelemetryUserService>();
services.AddTransient<ITimezoneService, TimezoneService>();
services.AddTransient<IUserService, UserService>();
services.AddTransient<IUserRoleService, UserRoleService>();
services.AddTransient<IWellService, WellService>();
2021-12-03 09:44:10 +05:00
services.AddTransient<IWellCompositeService, WellCompositeService>();
services.AddTransient<IWellOperationImportService, WellOperationImportService>();
services.AddTransient<IWellOperationService, WellOperationService>();
2022-03-17 16:56:13 +05:00
services.AddTransient<IScheduleReportService, ScheduleReportService>();
2021-09-10 11:28:57 +05:00
// 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
2021-12-20 15:17:09 +05:00
services.AddTransient<ICrudService<PermissionDto>, CrudCacheServiceBase<PermissionDto, Permission>>();
2021-09-10 11:28:57 +05:00
// TelemetryData services
services.AddTransient<ITelemetryDataService<TelemetryDataSaubDto>, TelemetryDataSaubService>();
services.AddTransient<ITelemetryDataService<TelemetryDataSpinDto>, TelemetryDataSpinService>();
services.AddValidators();
return services;
}
2021-12-01 11:49:59 +05:00
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)));
}
}