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

41 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using AsbCloudApp.Data;
using AsbCloudApp.Requests.ParserOptions;
using AsbCloudApp.Services.Parser;
using AsbCloudInfrastructure.Services.Trajectory.Parser;
namespace AsbCloudInfrastructure.Services;
public class ParserServiceFactory
{
public const int IdTrajectoryFactManualParserService = 1;
public const int IdTrajectoryPlanParserService = 2;
private readonly IDictionary<int, Func<object>> parsers = new Dictionary<int, Func<object>>
{
{ IdTrajectoryPlanParserService, () => new TrajectoryPlanParserService() },
{ IdTrajectoryFactManualParserService, () => new TrajectoryFactManualParserService() }
};
public IParserService<TDto> GetParser<TDto>(int idParserService)
where TDto : class, IId
{
if (!parsers.TryGetValue(idParserService, out var parserService))
throw new ArgumentNullException(nameof(idParserService), "Сервис не зарегистрирован");
return parserService.Invoke() as IParserService<TDto>
?? throw new ArgumentNullException(nameof(idParserService), "Ошибка приведения типа");
}
public IParserServiceWithOptions<TDto, TOptions> GetParserWithOptions<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 IParserServiceWithOptions<TDto, TOptions>
?? throw new ArgumentNullException(nameof(idParserService), "Ошибка приведения типа");
}
}