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; public class ParserServiceFactory : IDisposable { public const int IdTrajectoryFactManualParser = 1; public const int IdTrajectoryPlanParser = 2; public const int IdProcessMapPlanDrillingParser = 3; private readonly IDictionary> parsers; private readonly IServiceScope serviceScope; public ParserServiceFactory(IServiceProvider serviceProvider) { serviceScope = serviceProvider.CreateScope(); parsers = new Dictionary> { { IdTrajectoryPlanParser, () => new TrajectoryPlanParser(serviceScope.ServiceProvider) }, { IdTrajectoryFactManualParser, () => new TrajectoryFactManualParser(serviceScope.ServiceProvider) }, { IdProcessMapPlanDrillingParser, () => new ProcessMapPlanDrillingParser(serviceScope.ServiceProvider) } }; } public IParserService Create(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 ?? throw new ArgumentNullException(nameof(idParserService), "Ошибка приведения типа"); } public void Dispose() { serviceScope.Dispose(); } }