forked from ddrilling/AsbCloudServer
Рефакторинг сервисов парсинга
This commit is contained in:
parent
8a429cfe8b
commit
63e2e1b180
@ -8,10 +8,8 @@ namespace AsbCloudApp.Services;
|
||||
/// Сервис парсинга
|
||||
/// </summary>
|
||||
/// <typeparam name="TDto"></typeparam>
|
||||
/// <typeparam name="TOptions"></typeparam>
|
||||
public interface IParserService<TDto, in TOptions> : IParserService
|
||||
public interface IParserService<TDto> : IParserService
|
||||
where TDto : class, IId
|
||||
where TOptions : IParserOptionsRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Распарсить файл
|
||||
@ -19,7 +17,8 @@ public interface IParserService<TDto, in TOptions> : IParserService
|
||||
/// <param name="file"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
ParserResultDto<TDto> Parse(Stream file, TOptions options);
|
||||
ParserResultDto<TDto> Parse<TOptions>(Stream file, TOptions options)
|
||||
where TOptions : IParserOptionsRequest;
|
||||
|
||||
/// <summary>
|
||||
/// Получение шаблона для заполнения
|
||||
|
@ -332,21 +332,11 @@ namespace AsbCloudInfrastructure
|
||||
services.AddTransient<IWellSectionPlanRepository, WellSectionPlanRepository>();
|
||||
services.AddTransient<IWellOperationCategoryRepository, WellOperationCategoryRepository>();
|
||||
services.AddTransient<IDetectedOperationRepository, DetectedOperationRepository>();
|
||||
|
||||
|
||||
services.AddTransient<ParserServiceFactory>(serviceProvider =>
|
||||
{
|
||||
var parsers = new Dictionary<int, Func<IParserService>>
|
||||
{
|
||||
{ ParserIds.IdTrajectoryPlanParser, () => new TrajectoryPlanParser(serviceProvider) },
|
||||
{ ParserIds.IdTrajectoryFactManualParser, () => new TrajectoryFactManualParser(serviceProvider) },
|
||||
{ ParserIds.IdProcessMapPlanDrillingParser, () => new ProcessMapPlanDrillingParser(serviceProvider) }
|
||||
};
|
||||
|
||||
var factory = new ParserServiceFactory(parsers);
|
||||
return factory;
|
||||
});
|
||||
|
||||
services.AddTransient<TrajectoryPlanParser>();
|
||||
services.AddTransient<TrajectoryFactManualParser>();
|
||||
services.AddTransient<ProcessMapPlanDrillingParser>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
@ -12,17 +12,9 @@ using Mapster;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services.Parser;
|
||||
|
||||
public abstract class ParserExcelService<TDto, TOptions> : IParserService<TDto, TOptions>
|
||||
public abstract class ParserExcelService<TDto> : IParserService<TDto>
|
||||
where TDto : class, IValidatableObject, IId
|
||||
where TOptions : IParserOptionsRequest
|
||||
{
|
||||
protected readonly IServiceProvider serviceProvider;
|
||||
|
||||
protected ParserExcelService(IServiceProvider serviceProvider)
|
||||
{
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
protected abstract string SheetName { get; }
|
||||
|
||||
protected virtual int HeaderRowsCount => 0;
|
||||
@ -31,7 +23,8 @@ public abstract class ParserExcelService<TDto, TOptions> : IParserService<TDto,
|
||||
|
||||
protected abstract IDictionary<string, Cell> Cells { get; }
|
||||
|
||||
public virtual ParserResultDto<TDto> Parse(Stream file, TOptions options)
|
||||
public virtual ParserResultDto<TDto> Parse<TOptions>(Stream file, TOptions options)
|
||||
where TOptions : IParserOptionsRequest
|
||||
{
|
||||
using var workbook = new XLWorkbook(file);
|
||||
var sheet = workbook.GetWorksheet(SheetName);
|
||||
|
@ -1,8 +0,0 @@
|
||||
namespace AsbCloudInfrastructure.Services.Parser;
|
||||
|
||||
public static class ParserIds
|
||||
{
|
||||
public const int IdTrajectoryFactManualParser = 1;
|
||||
public const int IdTrajectoryPlanParser = 2;
|
||||
public const int IdProcessMapPlanDrillingParser = 3;
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Requests.ParserOptions;
|
||||
using AsbCloudApp.Services;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services.Parser;
|
||||
|
||||
public class ParserServiceFactory
|
||||
{
|
||||
private readonly IDictionary<int, Func<IParserService>> parsers;
|
||||
|
||||
public ParserServiceFactory(IDictionary<int, Func<IParserService>> parsers)
|
||||
{
|
||||
this.parsers = parsers;
|
||||
}
|
||||
|
||||
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), "Ошибка приведения типа");
|
||||
}
|
||||
}
|
@ -14,11 +14,8 @@ public class ProcessMapPlanDrillingParser : ProcessMapPlanParser<ProcessMapPlanD
|
||||
{
|
||||
private readonly IEnumerable<WellSectionTypeDto> sections;
|
||||
|
||||
public ProcessMapPlanDrillingParser(IServiceProvider serviceProvider)
|
||||
: base(serviceProvider)
|
||||
public ProcessMapPlanDrillingParser(IWellOperationRepository wellOperationRepository)
|
||||
{
|
||||
var wellOperationRepository = serviceProvider.GetRequiredService<IWellOperationRepository>();
|
||||
|
||||
sections = wellOperationRepository.GetSectionTypes();
|
||||
}
|
||||
|
||||
|
@ -1,20 +1,12 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Data.ProcessMapPlan;
|
||||
using AsbCloudApp.Requests.ParserOptions;
|
||||
using AsbCloudInfrastructure.Services.Parser;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services.ProcessMapPlan.Parser;
|
||||
|
||||
public abstract class ProcessMapPlanParser<TDto> : ParserExcelService<TDto, IParserOptionsRequest>
|
||||
public abstract class ProcessMapPlanParser<TDto> : ParserExcelService<TDto>
|
||||
where TDto : ProcessMapPlanBaseDto
|
||||
{
|
||||
protected ProcessMapPlanParser(IServiceProvider serviceProvider)
|
||||
: base(serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
protected override int HeaderRowsCount => 2;
|
||||
|
||||
protected static int? GetIdMode(string? modeName) =>
|
||||
|
@ -1,18 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using AsbCloudApp.Data.Trajectory;
|
||||
using AsbCloudApp.Requests.ParserOptions;
|
||||
using AsbCloudInfrastructure.Services.Parser;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services.Trajectory.Parser;
|
||||
|
||||
public class TrajectoryFactManualParser : ParserExcelService<TrajectoryGeoFactDto, IParserOptionsRequest>
|
||||
public class TrajectoryFactManualParser : ParserExcelService<TrajectoryGeoFactDto>
|
||||
{
|
||||
public TrajectoryFactManualParser(IServiceProvider serviceProvider)
|
||||
: base(serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
protected override string SheetName => "Фактическая траектория";
|
||||
|
||||
protected override int HeaderRowsCount => 2;
|
||||
|
@ -1,18 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using AsbCloudApp.Data.Trajectory;
|
||||
using AsbCloudApp.Requests.ParserOptions;
|
||||
using AsbCloudInfrastructure.Services.Parser;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services.Trajectory.Parser;
|
||||
|
||||
public class TrajectoryPlanParser : ParserExcelService<TrajectoryGeoPlanDto, IParserOptionsRequest>
|
||||
public class TrajectoryPlanParser : ParserExcelService<TrajectoryGeoPlanDto>
|
||||
{
|
||||
public TrajectoryPlanParser(IServiceProvider serviceProvider)
|
||||
: base(serviceProvider)
|
||||
{
|
||||
}
|
||||
|
||||
protected override string SheetName => "Плановая траектория";
|
||||
|
||||
protected override int HeaderRowsCount => 2;
|
||||
|
@ -2,6 +2,7 @@
|
||||
using AsbCloudApp.Data.Trajectory;
|
||||
using AsbCloudApp.Requests.ParserOptions;
|
||||
using AsbCloudInfrastructure.Services.Parser;
|
||||
using AsbCloudInfrastructure.Services.Trajectory.Parser;
|
||||
using Xunit;
|
||||
|
||||
namespace AsbCloudWebApi.Tests.Services.Trajectory;
|
||||
@ -10,8 +11,9 @@ public class TrajectoryParserTest
|
||||
{
|
||||
private const string UsingTemplateFile = "AsbCloudWebApi.Tests.Services.Trajectory.Templates";
|
||||
|
||||
private readonly ParserServiceFactory parserServiceFactory;
|
||||
|
||||
private readonly TrajectoryPlanParser trajectoryPlanParser = new();
|
||||
private readonly TrajectoryFactManualParser trajectoryFactManualParser = new();
|
||||
|
||||
[Fact]
|
||||
public void Parse_trajectory_plan()
|
||||
{
|
||||
@ -20,11 +22,8 @@ public class TrajectoryParserTest
|
||||
|
||||
if (stream is null)
|
||||
Assert.Fail("Файла для импорта не существует");
|
||||
|
||||
var parserService = parserServiceFactory.Create<TrajectoryGeoPlanDto, IParserOptionsRequest>(
|
||||
ParserIds.IdTrajectoryPlanParser);
|
||||
|
||||
var trajectoryRows = parserService.Parse(stream, IParserOptionsRequest.Empty());
|
||||
var trajectoryRows = trajectoryPlanParser.Parse(stream, IParserOptionsRequest.Empty());
|
||||
|
||||
Assert.Equal(3, trajectoryRows.Item.Count());
|
||||
}
|
||||
@ -38,10 +37,7 @@ public class TrajectoryParserTest
|
||||
if (stream is null)
|
||||
Assert.Fail("Файла для импорта не существует");
|
||||
|
||||
var parserService = parserServiceFactory.Create<TrajectoryGeoFactDto, IParserOptionsRequest>(
|
||||
ParserIds.IdTrajectoryFactManualParser);
|
||||
|
||||
var trajectoryRows = parserService.Parse(stream, IParserOptionsRequest.Empty());
|
||||
var trajectoryRows = trajectoryFactManualParser.Parse(stream, IParserOptionsRequest.Empty());
|
||||
|
||||
Assert.Equal(4, trajectoryRows.Item.Count());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user