diff --git a/sources/core/Crypto.Compare.Services/BackgroundJobs/SymbolProviderUpdateDatabaseQueueService.cs b/sources/core/Crypto.Compare.Services/BackgroundJobs/SymbolProviderUpdateDatabaseQueueService.cs index a39f2b4..b79a431 100644 --- a/sources/core/Crypto.Compare.Services/BackgroundJobs/SymbolProviderUpdateDatabaseQueueService.cs +++ b/sources/core/Crypto.Compare.Services/BackgroundJobs/SymbolProviderUpdateDatabaseQueueService.cs @@ -1,5 +1,6 @@ using Crypto.Compare.Adapter.AdaptersObservable; using Crypto.Compare.DataAccess; +using EFCore.BulkExtensions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; @@ -15,6 +16,9 @@ public class SymbolProviderUpdateDatabaseQueueService : BackgroundService /// private const int WaitTimeout = 3000; + private static List propertyExclude = new() { "Symbol" }; + + private readonly ISymbolProviderUpdateDatabaseQueue _symbolProviderUpdateDatabaseQueue; private readonly ILogger _logger; private readonly IServiceScopeFactory _serviceScopeFactory; @@ -29,7 +33,7 @@ public SymbolProviderUpdateDatabaseQueueService(ISymbolProviderUpdateDatabaseQue protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Updated database symbols provider Background Service is starting."); - + using var scope = _serviceScopeFactory.CreateScope(); await using var dbContext = scope.ServiceProvider.GetService(); @@ -44,14 +48,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) continue; } - + await dbContext.BulkUpdateAsync(models, option => { - option.IgnoreOnUpdateExpression = c => new { c.Symbol }; - }, stoppingToken ); + option.PropertiesToExclude = propertyExclude; + }, cancellationToken: stoppingToken); _logger.LogDebug("Update prices database for: {Count} symbols", models.Count); - + } catch (Exception e) { diff --git a/sources/core/Crypto.Compare.Services/Crypto.Compare.Services.csproj b/sources/core/Crypto.Compare.Services/Crypto.Compare.Services.csproj index 2668267..aa8f946 100644 --- a/sources/core/Crypto.Compare.Services/Crypto.Compare.Services.csproj +++ b/sources/core/Crypto.Compare.Services/Crypto.Compare.Services.csproj @@ -7,12 +7,13 @@ + + - + - + - diff --git a/sources/core/Crypto.Compare.Services/Results/SymbolProviders/SymbolProviderResult.cs b/sources/core/Crypto.Compare.Services/Results/SymbolProviders/SymbolProviderResult.cs index 1553a56..0089583 100644 --- a/sources/core/Crypto.Compare.Services/Results/SymbolProviders/SymbolProviderResult.cs +++ b/sources/core/Crypto.Compare.Services/Results/SymbolProviders/SymbolProviderResult.cs @@ -1,10 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Crypto.Compare.Services.Results.SymbolProviders; -namespace Crypto.Compare.Services.Results.SymbolProviders; - -public class SymbolProviderResult +public class SymbolProviderResult : IHandlerResult { /// diff --git a/sources/core/Crypto.Compare.Services/Results/Symbols/GetSymbolsResult.cs b/sources/core/Crypto.Compare.Services/Results/Symbols/GetSymbolsResult.cs index fbe6741..9ee52e2 100644 --- a/sources/core/Crypto.Compare.Services/Results/Symbols/GetSymbolsResult.cs +++ b/sources/core/Crypto.Compare.Services/Results/Symbols/GetSymbolsResult.cs @@ -1,5 +1,4 @@ -using Crypto.Compare.Data; -namespace Crypto.Compare.Services.Results.Symbols; +namespace Crypto.Compare.Services.Results.Symbols; public class GetSymbolsResult : IHandlerResult { diff --git a/sources/core/Crypto.Compare.Services/Results/Symbols/SymbolResult.cs b/sources/core/Crypto.Compare.Services/Results/Symbols/SymbolResult.cs index a0514a7..b8cf2db 100644 --- a/sources/core/Crypto.Compare.Services/Results/Symbols/SymbolResult.cs +++ b/sources/core/Crypto.Compare.Services/Results/Symbols/SymbolResult.cs @@ -1,6 +1,6 @@ namespace Crypto.Compare.Services.Results.Symbols; -public class SymbolResult +public class SymbolResult : IHandlerResult { /// /// Uniq id diff --git a/sources/infrastructure/Crypto.Compare.DataAccess/Crypto.Compare.DataAccess.csproj b/sources/infrastructure/Crypto.Compare.DataAccess/Crypto.Compare.DataAccess.csproj index 9eb58a8..a40eccd 100644 --- a/sources/infrastructure/Crypto.Compare.DataAccess/Crypto.Compare.DataAccess.csproj +++ b/sources/infrastructure/Crypto.Compare.DataAccess/Crypto.Compare.DataAccess.csproj @@ -6,10 +6,10 @@ enable - - - - + + + + diff --git a/sources/integrations/Crypto.Compare.BingX/Crypto.Compare.BingX.csproj b/sources/integrations/Crypto.Compare.BingX/Crypto.Compare.BingX.csproj index 34822fd..1941b03 100644 --- a/sources/integrations/Crypto.Compare.BingX/Crypto.Compare.BingX.csproj +++ b/sources/integrations/Crypto.Compare.BingX/Crypto.Compare.BingX.csproj @@ -7,7 +7,7 @@ - + diff --git a/sources/integrations/Crypto.Compare.BitMart/Crypto.Compare.BitMart.csproj b/sources/integrations/Crypto.Compare.BitMart/Crypto.Compare.BitMart.csproj index b52680d..3a30df7 100644 --- a/sources/integrations/Crypto.Compare.BitMart/Crypto.Compare.BitMart.csproj +++ b/sources/integrations/Crypto.Compare.BitMart/Crypto.Compare.BitMart.csproj @@ -12,8 +12,7 @@ - - + diff --git a/sources/integrations/Crypto.Compare.Bitget/Crypto.Compare.Bitget.csproj b/sources/integrations/Crypto.Compare.Bitget/Crypto.Compare.Bitget.csproj index 7ad039c..ddcf034 100644 --- a/sources/integrations/Crypto.Compare.Bitget/Crypto.Compare.Bitget.csproj +++ b/sources/integrations/Crypto.Compare.Bitget/Crypto.Compare.Bitget.csproj @@ -7,7 +7,7 @@ - + diff --git a/sources/integrations/Crypto.Compare.ByBit/Crypto.Compare.ByBit.csproj b/sources/integrations/Crypto.Compare.ByBit/Crypto.Compare.ByBit.csproj index 54f846a..c264857 100644 --- a/sources/integrations/Crypto.Compare.ByBit/Crypto.Compare.ByBit.csproj +++ b/sources/integrations/Crypto.Compare.ByBit/Crypto.Compare.ByBit.csproj @@ -7,7 +7,7 @@ - + diff --git a/sources/integrations/Crypto.Compare.GateIo/Crypto.Compare.GateIo.csproj b/sources/integrations/Crypto.Compare.GateIo/Crypto.Compare.GateIo.csproj index 85f825b..13f1932 100644 --- a/sources/integrations/Crypto.Compare.GateIo/Crypto.Compare.GateIo.csproj +++ b/sources/integrations/Crypto.Compare.GateIo/Crypto.Compare.GateIo.csproj @@ -7,7 +7,7 @@ - + diff --git a/sources/integrations/Crypto.Compare.Mexc/Crypto.Compare.Mexc.csproj b/sources/integrations/Crypto.Compare.Mexc/Crypto.Compare.Mexc.csproj index 830a360..4a34681 100644 --- a/sources/integrations/Crypto.Compare.Mexc/Crypto.Compare.Mexc.csproj +++ b/sources/integrations/Crypto.Compare.Mexc/Crypto.Compare.Mexc.csproj @@ -11,7 +11,7 @@ - + diff --git a/sources/integrations/Crypto.Compare.Xtcom/Crypto.Compare.Xtcom.csproj b/sources/integrations/Crypto.Compare.Xtcom/Crypto.Compare.Xtcom.csproj index ac3e9bd..700b47f 100644 --- a/sources/integrations/Crypto.Compare.Xtcom/Crypto.Compare.Xtcom.csproj +++ b/sources/integrations/Crypto.Compare.Xtcom/Crypto.Compare.Xtcom.csproj @@ -10,7 +10,7 @@ - + diff --git a/sources/presentation/Crypto.Compare.PublicApi/Crypto.Compare.PublicApi.csproj b/sources/presentation/Crypto.Compare.PublicApi/Crypto.Compare.PublicApi.csproj index 184f3d3..e88ea21 100644 --- a/sources/presentation/Crypto.Compare.PublicApi/Crypto.Compare.PublicApi.csproj +++ b/sources/presentation/Crypto.Compare.PublicApi/Crypto.Compare.PublicApi.csproj @@ -5,23 +5,25 @@ enable enable Linux - 1.0.100.$([System.DateTime]::UtcNow.ToString("ddHH")) + 1.0.101.$([System.DateTime]::UtcNow.ToString("ddHH")) true true - - + - - + + + + + - - - - + + + + diff --git a/sources/presentation/Crypto.Compare.PublicApi/Ioc/ServicesRegistry.cs b/sources/presentation/Crypto.Compare.PublicApi/Ioc/ServicesRegistry.cs index aff01e5..64ea9f7 100644 --- a/sources/presentation/Crypto.Compare.PublicApi/Ioc/ServicesRegistry.cs +++ b/sources/presentation/Crypto.Compare.PublicApi/Ioc/ServicesRegistry.cs @@ -1,5 +1,4 @@ -using AutoMapper; -using Crypto.Compare.Adapter; +using Crypto.Compare.Adapter; using Crypto.Compare.Adapter.AdaptersObservable; using Crypto.Compare.Adapter.Impl; using Crypto.Compare.BingX; @@ -34,6 +33,8 @@ using Crypto.Compare.Xtcom; using CryptoExchange.Net.Authentication; using FluentResults; +using Mapster; +using MapsterMapper; using MediatR; using Microsoft.EntityFrameworkCore; using System.Reflection; @@ -48,9 +49,22 @@ internal static IServiceCollection AddServices(this IServiceCollection services, .AddConfigurations(configuration) .AddMediator() .AddSingletons() + .AddMapper() .AddExternalServices() - .AddAdapters(configuration) - ; + .AddAdapters(configuration); + + + } + + private static IServiceCollection AddMapper(this IServiceCollection services) + { + // Mapster + var config = TypeAdapterConfig.GlobalSettings; + config.Apply(new MapsterProfile()); + services.AddSingleton(config); + services.AddSingleton(); + services.AddSingleton(); + return services; } private static IServiceCollection AddConfigurations(this IServiceCollection services, IConfiguration configuration) @@ -72,7 +86,7 @@ private static IServiceCollection AddAdapters(this IServiceCollection services, services.AddSingleton(); services.AddSingleton(); - + services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); @@ -82,8 +96,8 @@ private static IServiceCollection AddAdapters(this IServiceCollection services, services.AddSingleton(); services.AddSingleton(); - - services.AddSingleton(); + + services.AddSingleton(); services.AddHostedService(); @@ -229,8 +243,6 @@ private static (string? ApiKey, string? ApiSecret, string? passwordKey) LoadCred private static IServiceCollection AddSingletons(this IServiceCollection services) { return services - .AddSingleton() - .AddSingleton(_ => new MapperConfiguration(cfg => { cfg.AddProfile(); }).CreateMapper()) .AddSingleton() .AddSingleton() .AddSingleton() @@ -253,7 +265,7 @@ private static IServiceCollection AddMediator(this IServiceCollection services) .AddScoped>, GetProviderQueryHandler>() .AddScoped>, GetProvidersQueryHandler>() .AddScoped>, GetSymbolProvidersQueryHandler>() - + ; } } diff --git a/sources/presentation/Crypto.Compare.PublicApi/Mapping/AutoMapperProfile.cs b/sources/presentation/Crypto.Compare.PublicApi/Mapping/AutoMapperProfile.cs deleted file mode 100644 index 8acf1f4..0000000 --- a/sources/presentation/Crypto.Compare.PublicApi/Mapping/AutoMapperProfile.cs +++ /dev/null @@ -1,31 +0,0 @@ -using AutoMapper; -using Crypto.Compare.Data; -using Crypto.Compare.PublicApi.Dtos; -using Crypto.Compare.PublicApi.Responses.v1.Providers; -using Crypto.Compare.PublicApi.Responses.v1.SymbolProvider; -using Crypto.Compare.PublicApi.Responses.v1.Symbols; -using Crypto.Compare.Services.Results.Providers; -using Crypto.Compare.Services.Results.SymbolProviders; -using Crypto.Compare.Services.Results.Symbols; - -namespace Crypto.Compare.PublicApi.Mapping; - -/// -/// Mapping results from Mediator to Responses -/// -public class AutoMapperProfile : Profile -{ - public AutoMapperProfile() - { - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - - CreateMap(); - CreateMap(); - CreateMap(); - } -} \ No newline at end of file diff --git a/sources/presentation/Crypto.Compare.PublicApi/Mapping/MapsterProfile.cs b/sources/presentation/Crypto.Compare.PublicApi/Mapping/MapsterProfile.cs new file mode 100644 index 0000000..1c08951 --- /dev/null +++ b/sources/presentation/Crypto.Compare.PublicApi/Mapping/MapsterProfile.cs @@ -0,0 +1,29 @@ +using Crypto.Compare.Data; +using Crypto.Compare.PublicApi.Dtos; +using Crypto.Compare.PublicApi.Responses.v1.Providers; +using Crypto.Compare.PublicApi.Responses.v1.SymbolProvider; +using Crypto.Compare.PublicApi.Responses.v1.Symbols; +using Crypto.Compare.Services.Results.Providers; +using Crypto.Compare.Services.Results.SymbolProviders; +using Crypto.Compare.Services.Results.Symbols; +using Mapster; + +namespace Crypto.Compare.PublicApi.Mapping; + +public class MapsterProfile : IRegister +{ + public void Register(TypeAdapterConfig config) + { + // Простые маппинги (аналогичны CreateMap) + config.NewConfig(); + config.NewConfig(); + config.NewConfig(); + config.NewConfig(); + config.NewConfig(); + config.NewConfig(); + + config.NewConfig(); + config.NewConfig(); + config.NewConfig(); + } +} diff --git a/sources/presentation/Crypto.Compare.PublicApi/Mapping/ResponseMapper.cs b/sources/presentation/Crypto.Compare.PublicApi/Mapping/ResponseMapper.cs index 7a9d7c1..7db9c4b 100644 --- a/sources/presentation/Crypto.Compare.PublicApi/Mapping/ResponseMapper.cs +++ b/sources/presentation/Crypto.Compare.PublicApi/Mapping/ResponseMapper.cs @@ -1,7 +1,7 @@ -using AutoMapper; -using Crypto.Compare.PublicApi.Extensions; +using Crypto.Compare.PublicApi.Extensions; using Crypto.Compare.PublicApi.Responses; using FluentResults; +using MapsterMapper; using Microsoft.AspNetCore.Mvc; namespace Crypto.Compare.PublicApi.Mapping; diff --git a/sources/presentation/Crypto.Compare.PublicApi/Program.cs b/sources/presentation/Crypto.Compare.PublicApi/Program.cs index fe17d99..36b337a 100644 --- a/sources/presentation/Crypto.Compare.PublicApi/Program.cs +++ b/sources/presentation/Crypto.Compare.PublicApi/Program.cs @@ -6,7 +6,6 @@ using System.Reflection; using System.Text.Json.Serialization; using Microsoft.OpenApi; -using Microsoft.OpenApi.Models; using ApiRequestValidationFilter = Crypto.Compare.PublicApi.Filters.ApiRequestValidationFilter; var builder = WebApplication.CreateBuilder(args); diff --git a/tests/Crypto.Compare.PublicApi.Tests/AutoMapperTests.cs b/tests/Crypto.Compare.PublicApi.Tests/AutoMapperTests.cs index be5cec3..4fa1e36 100644 --- a/tests/Crypto.Compare.PublicApi.Tests/AutoMapperTests.cs +++ b/tests/Crypto.Compare.PublicApi.Tests/AutoMapperTests.cs @@ -1,10 +1,12 @@ -using System.Reflection; -using AutoMapper; -using Crypto.Compare.PublicApi.Controllers; +using Crypto.Compare.PublicApi.Controllers; using Crypto.Compare.PublicApi.Mapping; using Crypto.Compare.Services.Results; using FluentAssertions; +using Mapster; +using MapsterMapper; +using Microsoft.Extensions.DependencyInjection; using NUnit.Framework; +using System.Reflection; namespace Crypto.Compare.PublicApi.Tests; @@ -13,10 +15,20 @@ public class AutoMapperTests [Test] public void HandlerResult_Should_Have_Response() { - var mapper = new MapperConfiguration(cfg => cfg.AddProfile()).CreateMapper(); + // Create ServiceProvider + var services = new ServiceCollection(); + services.AddSingleton(TypeAdapterConfig.GlobalSettings); + services.AddScoped(); + var serviceProvider = services.BuildServiceProvider(); + + // register profile + TypeAdapterConfig.GlobalSettings.Apply(new MapsterProfile()); + + // Get IMapper + IMapper _automapper = serviceProvider.GetRequiredService(); var assembly = Assembly.GetAssembly(typeof(HomeController)); - var handlerResultTypes = GetResultsTypes( Assembly.GetAssembly(typeof(IHandlerResult))); + var handlerResultTypes = GetResultsTypes(Assembly.GetAssembly(typeof(IHandlerResult))).ToList(); var responsesTypes = GetResponseTypes(assembly).ToList(); foreach (var handlerResultType in handlerResultTypes) @@ -27,11 +39,19 @@ public void HandlerResult_Should_Have_Response() var expectedResponseName = baseName + "Response"; var responseType = responsesTypes.FirstOrDefault(x => x.Name == expectedResponseName); + if(responseType == null) + { + // check end Dto + expectedResponseName = baseName + "Dto"; + responseType = responsesTypes.FirstOrDefault(x => x.Name == expectedResponseName); + } responseType.Should().NotBeNull($"Not exists name response: {expectedResponseName}"); var responseObj = Activator.CreateInstance(responseType); var resultObj = Activator.CreateInstance(handlerResultType); - var mapped = mapper.Map(resultObj, responseObj); + + var mapped = _automapper.Map(resultObj, resultObj.GetType(), responseType); + //var mapped = _automapper.Adapt(resultObj.GetType(), responseType); mapped.Should().NotBeNull($"Not mapping {handlerResultType.Name} => {responseType.Name}"); } diff --git a/tests/Crypto.Compare.PublicApi.Tests/Crypto.Compare.PublicApi.Tests.csproj b/tests/Crypto.Compare.PublicApi.Tests/Crypto.Compare.PublicApi.Tests.csproj index 15f13ef..aafbce2 100644 --- a/tests/Crypto.Compare.PublicApi.Tests/Crypto.Compare.PublicApi.Tests.csproj +++ b/tests/Crypto.Compare.PublicApi.Tests/Crypto.Compare.PublicApi.Tests.csproj @@ -10,15 +10,19 @@ - - - + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/tests/Crypto.Compare.Services.Tests/Crypto.Compare.Services.Tests.csproj b/tests/Crypto.Compare.Services.Tests/Crypto.Compare.Services.Tests.csproj index b39358a..4333e04 100644 --- a/tests/Crypto.Compare.Services.Tests/Crypto.Compare.Services.Tests.csproj +++ b/tests/Crypto.Compare.Services.Tests/Crypto.Compare.Services.Tests.csproj @@ -8,16 +8,16 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + +