replace automapper to mapster

This commit is contained in:
Фролов 2021-07-28 09:46:58 +05:00
parent 1ab8ae89ac
commit 38b21d80f1
11 changed files with 88 additions and 54 deletions

View File

@ -7,7 +7,7 @@ namespace AsbCloudApp.Data
public int Id { get; set; }
public string Name { get; set; }
public string FullName { get; set; }
public int idWell { get; set; }
public int IdWell { get; set; }
public DateTime Date { get; set; }
public DateTimeOffset Begin { get; set; }
public DateTimeOffset End { get; set; }

View File

@ -8,5 +8,17 @@ namespace AsbCloudApp.Data
{
public class WellOperationDto
{
public int Id { get; set; }
public int IdWell { get; set; }
public string Caption { get; set; }
public string Description { get; set; }
public double CasingSection { get; set; }
public double WellDepth { get; set; }
}
}

View File

@ -32,11 +32,10 @@ namespace AsbCloudDb.Model
public virtual DbSet<WellOperation> WellOperations { get; set; }
public virtual DbSet<WellType> WellTypes { get; set; }
//public AsbCloudDbContext(string connectionString = "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
//{
// this.connectionString = connectionString;
// Database.EnsureCreated();
//}
//var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
// .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
// .Options;
//var context = new AsbCloudDbContext(options);
public AsbCloudDbContext(DbContextOptions<AsbCloudDbContext> options)
: base(options)

View File

@ -9,8 +9,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="itext7" Version="7.1.15" />
<PackageReference Include="Mapster" Version="7.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.10.0" />
</ItemGroup>

View File

@ -3,7 +3,6 @@ using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
using AsbCloudInfrastructure.Services.Cache;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -21,7 +20,6 @@ namespace AsbCloudInfrastructure
services.AddHostedService<BackgroundWorkerService>();
services.AddSingleton(new MapperConfiguration(AutoMapperConfig));
services.AddSingleton(new CacheDb());
services.AddSingleton<ITelemetryTracker, TelemetryTracker>();
services.AddSingleton<IBackgroundQueue, BackgroundQueue>();
@ -42,16 +40,5 @@ namespace AsbCloudInfrastructure
return services;
}
private static void AutoMapperConfig(IMapperConfigurationExpression cfg)
{
cfg.CreateMap<DataSaubBase, DataSaubBaseDto>();
cfg.CreateMap<DataSaubBaseDto, DataSaubBase>();
cfg.CreateMap<Message, TelemetryMessageDto>();
cfg.CreateMap<TelemetryMessageDto, Message>();
cfg.CreateMap<TelemetryInfo, TelemetryInfoDto>();
cfg.CreateMap<TelemetryInfoDto, TelemetryInfo>();
}
}
}

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Mapster
{
public static class MapsterExtension
{
public static IEnumerable<TDestination> Adapt<TDestination>(this IEnumerable<object> sourceList)
{
foreach (var item in sourceList)
yield return item.Adapt<TDestination>();
}
public static TDestination Adapt<TSource, TDestination>(this TSource source, Action<TSource, TDestination> afterMapAction = default)
{
var dest = source.Adapt<TDestination>();
if (afterMapAction != default)
afterMapAction(source, dest);
return dest;
}
public static IEnumerable<TDestination> Adapt<TSource, TDestination>(this IEnumerable<TSource> sourceList, Action<TSource, TDestination> eachAfterMapAction = default)
{
foreach (var item in sourceList)
yield return item.Adapt(eachAfterMapAction);
}
}
}

View File

@ -2,7 +2,7 @@
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using AutoMapper;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
@ -14,18 +14,15 @@ namespace AsbCloudInfrastructure.Services
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
private readonly IAnalyticsService analyticsService;
private readonly IMapper mapper;
private readonly CacheTable<Telemetry> cacheTelemetry;
private readonly CacheTable<Well> cacheWells;
public DataService(IAsbCloudDbContext db, ITelemetryService telemetryService,
IAnalyticsService analyticsService, CacheDb cacheDb,
MapperConfiguration mapperConfiguration)
IAnalyticsService analyticsService, CacheDb cacheDb)
{
this.db = db;
this.telemetryService = telemetryService;
this.analyticsService = analyticsService;
mapper = mapperConfiguration.CreateMapper();
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
@ -45,12 +42,12 @@ namespace AsbCloudInfrastructure.Services
var datEnd = dateBegin.AddSeconds(intervalSec);
var fullData = from data in db.DataSaubBases
var query = from data in db.DataSaubBases
where data.IdTelemetry == telemetry.Id
&& data.Date >= dateBegin && data.Date < datEnd
select data;
var fullDataCount = fullData.Count();
var fullDataCount = query.Count();
if (fullDataCount == 0)
return default;
@ -59,16 +56,14 @@ namespace AsbCloudInfrastructure.Services
{
var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
if (m > 1)
fullData = fullData.Where(d => d.Id % m == 0);
query = query.Where(d => d.Id % m == 0);
}
var dbData = fullData.ToList();
var result = new List<DataSaubBaseDto>(dbData.Count);
var entities = query.ToList();
foreach (var item in dbData)
result.Add(mapper.Map<DataSaubBaseDto>(item));
var dtos = entities.Adapt<DataSaubBaseDto>();
return result;
return dtos;
}
public void UpdateData(string uid, IEnumerable<DataSaubBaseDto> dtos)
@ -87,13 +82,12 @@ namespace AsbCloudInfrastructure.Services
select d).ToList();
if (oldDataSaubBase.Any())
{
db.DataSaubBases.RemoveRange(oldDataSaubBase);
}
foreach (var item in dtos)
{
var dataSaub = mapper.Map<DataSaubBase>(item);
var dataSaub = item.Adapt<DataSaubBase>();
dataSaub.IdTelemetry = telemetryId;
db.DataSaubBases.Add(dataSaub);

View File

@ -2,7 +2,7 @@
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using AutoMapper;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
@ -14,15 +14,13 @@ namespace AsbCloudInfrastructure.Services
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
private readonly IMapper mapper;
private readonly CacheTable<Event> cacheEvents;
private readonly CacheTable<TelemetryUser> cacheTUsers;
public MessageService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService, MapperConfiguration mapperConfiguration)
public MessageService(IAsbCloudDbContext db, CacheDb cacheDb, ITelemetryService telemetryService)
{
this.db = db;
this.telemetryService = telemetryService;
mapper = mapperConfiguration.CreateMapper();
cacheEvents = cacheDb.GetCachedTable<Event>((AsbCloudDbContext)db);
cacheTUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
}
@ -118,7 +116,7 @@ namespace AsbCloudInfrastructure.Services
foreach (var dto in dtos)
{
var entity = mapper.Map<Message>(dto);
var entity = dto.Adapt<Message>();
entity.Id = 0;
entity.IdTelemetry = telemetryId;
db.Messages.Add(entity);

View File

@ -96,10 +96,10 @@ namespace AsbCloudInfrastructure.Services
var suitableReportsProperties = suitableReportsFromDb.Select(r => new ReportPropertiesDto
{
Id = r.Id,
Name = Path.GetFileName(r.File.Name),
FullName = r.File.Name,
idWell = r.IdWell,
Date = r.File.Date,
Name = Path.GetFileName(r.Name),
FullName = r.Name,
IdWell = r.IdWell,
Date = r.Date,
Begin = r.Begin,
End = r.End,
Step = r.Step,

View File

@ -2,19 +2,17 @@
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using AutoMapper;
using Mapster;
namespace AsbCloudInfrastructure.Services
{
public class TelemetryService : ITelemetryService
{
private readonly IMapper mapper;
private readonly CacheTable<Telemetry> cacheTelemetry;
private readonly CacheTable<Well> cacheWells;
public TelemetryService(IAsbCloudDbContext db, CacheDb cacheDb, MapperConfiguration mapperConfiguration)
public TelemetryService(IAsbCloudDbContext db, CacheDb cacheDb)
{
mapper = mapperConfiguration.CreateMapper();
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
@ -31,7 +29,7 @@ namespace AsbCloudInfrastructure.Services
public void UpdateInfo(string uid, TelemetryInfoDto info)
{
var telemetry = GetOrCreateTelemetryByUid(uid);
telemetry.Info = mapper.Map<TelemetryInfo>(info);
telemetry.Info = info.Adapt<TelemetryInfo>();
cacheTelemetry.Upsert(telemetry);
}

View File

@ -2,6 +2,7 @@
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
@ -46,14 +47,27 @@ namespace AsbCloudInfrastructure.Services
public IEnumerable<WellSectionDto> GetSections(int idWell)
{
//var query = from s in db.WellSections
// where s.IdWell ==
return new List<WellSectionDto>();
var entities = db
.WellSections
.Where(s => s.IdWell == idWell)
.ToList();
var dtos = entities.Adapt<WellSection, WellSectionDto>(
(s, d) => { d.SectionType = s.WellSectionType.Caption;});
return dtos;
}
public IEnumerable<WellOperationDto> GetOperations(int idWell)
{
throw new NotImplementedException();
var entities = db
.WellOperations
.Where(o => o.IdWell == idWell)
.ToList();
var dtos = entities.Adapt<WellOperationDto>();
return dtos;
}
private static WellDto From(Well well)