DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Parser/ParserServiceFactory.cs

48 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using AsbCloudApp.Data;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.ProcessMapPlan.Parser;
using AsbCloudInfrastructure.Services.Trajectory.Parser;
using Microsoft.Extensions.DependencyInjection;
namespace AsbCloudInfrastructure.Services.Parser;
public class ParserServiceFactory : IDisposable
{
public const int IdTrajectoryFactManualParser = 1;
public const int IdTrajectoryPlanParser = 2;
public const int IdProcessMapPlanDrillingParser = 3;
private readonly IDictionary<int, Func<IParserService>> parsers;
private readonly IServiceScope serviceScope;
public ParserServiceFactory(IServiceProvider serviceProvider)
{
serviceScope = serviceProvider.CreateScope();
parsers = new Dictionary<int, Func<IParserService>>
{
{ IdTrajectoryPlanParser, () => new TrajectoryPlanParser(serviceScope.ServiceProvider) },
{ IdTrajectoryFactManualParser, () => new TrajectoryFactManualParser(serviceScope.ServiceProvider) },
{ IdProcessMapPlanDrillingParser, () => new ProcessMapPlanDrillingParser(serviceScope.ServiceProvider) }
};
}
public IParserService<TDto, TOptions> Create<TDto, TOptions>(int idParserService)
where TDto : class, IId
where TOptions : IParserOptionsRequest
{
if (!parsers.TryGetValue(idParserService, out var parserService))
throw new ArgumentNullException(nameof(idParserService), "Не правильный идентификатор парсера");
return parserService.Invoke() as IParserService<TDto, TOptions>
?? throw new ArgumentNullException(nameof(idParserService), "Ошибка приведения типа");
}
public void Dispose()
{
serviceScope.Dispose();
}
}