Skip to content

Commit

Permalink
Merge pull request #74 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Improvement map performance
  • Loading branch information
Ali-YousefiTelori authored Nov 15, 2023
2 parents 02c31e5 + 2fdffb7 commit b927ebc
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.13" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.46</Version>
<Version>0.0.0.47</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.46</Version>
<Version>0.0.0.47</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet,aspcore,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using EasyMicroservices.Mapper.SerializerMapper.Providers;
using EasyMicroservices.Serialization.Newtonsoft.Json.Providers;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -294,7 +295,8 @@ public virtual IMapperProvider GetMapper()
{
var mapper = new CompileTimeMapperProvider(new SerializerMapperProvider(new NewtonsoftJsonProvider(new Newtonsoft.Json.JsonSerializerSettings()
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
Error = HandleDeserializationError
})));
if (MapperTypeAssembly != null)
{
Expand All @@ -311,6 +313,17 @@ public virtual IMapperProvider GetMapper()
return mapper;
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="errorArgs"></param>
public virtual void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)
{
//var currentError = errorArgs.ErrorContext.Error.Message;
errorArgs.ErrorContext.Handled = true;
}

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FilterRequestContract
/// <summary>
///
/// </summary>
public bool? IsDeleted { get; set; }
public bool? IsDeleted { get; set; } = false;
/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -57,10 +57,14 @@ public class FilterRequestContract
/// <summary>
///
/// </summary>
public string SortColumnName { get; set; }
public string SortColumnNames { get; set; }
/// <summary>
///
/// </summary>
public bool IsDescending { get; set; }
/// <summary>
/// text to search
/// </summary>
public string Text { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class SoftDeleteRequestContract<T> : DeleteRequestContract<T>
/// <summary>
///
/// </summary>
public bool IsDelete { get; set; }
public bool IsDelete { get; set; } = true;

/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.46</Version>
<Version>0.0.0.47</Version>
<Description>core contracts.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,contract,contracts,dto,dtos</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ public async Task<MessageContract<TContract>> GetBy<TEntity, TContract>(IEasyRea
var entityResult = await GetBy(easyReadableQueryable, predicate, query, true, cancellationToken);
if (!entityResult)
return entityResult.ToContract<TContract>();
var result = await _mapperProvider.MapAsync<TContract>(entityResult.Result);
ValidateMappedResult(ref result);
var result = await MapAsync<TContract, TEntity>(entityResult.Result);
return result;
}

Expand All @@ -238,8 +237,7 @@ protected async Task<MessageContract<TContract>> GetById<TEntity, TContract, TId
var entityResult = await GetById(easyReadableQueryable, idRequest, query, cancellationToken);
if (!entityResult)
return entityResult.ToContract<TContract>();
var result = await _mapperProvider.MapAsync<TContract>(entityResult.Result);
ValidateMappedResult(ref result);
var result = await MapAsync<TContract, TEntity>(entityResult.Result);
return result;
}

Expand All @@ -263,8 +261,7 @@ public async Task<MessageContract<TContract>> GetByUniqueIdentity<TEntity, TCont
var entityResult = await GetBy(queryable, query, cancellationToken);
if (!entityResult)
return entityResult.ToContract<TContract>();
var result = await _mapperProvider.MapAsync<TContract>(entityResult.Result);
ValidateMappedResult(ref result);
var result = await MapAsync<TContract, TEntity>(entityResult.Result);
return result;
}

Expand Down Expand Up @@ -335,8 +332,7 @@ protected async Task<ListMessageContract<TContract>> Filter<TEntity, TContract>(
var entityResult = await Filter(filterRequest, easyReadableQueryable, query, cancellationToken);
if (!entityResult)
return entityResult.ToAnotherListContract<TContract>();
var result = await _mapperProvider.MapToListAsync<TContract>(entityResult.Result);
ValidateMappedResult(ref result);
var result = await MapToListAsync<TContract, TEntity>(entityResult.Result);
return result;
}

Expand Down Expand Up @@ -377,8 +373,7 @@ protected async Task<ListMessageContract<TContract>> GetAll<TEntity, TContract>(
var entityResult = await GetAll(easyReadableQueryable, query, cancellationToken);
if (!entityResult)
return entityResult.ToAnotherListContract<TContract>();
var result = await _mapperProvider.MapToListAsync<TContract>(entityResult.Result);
ValidateMappedResult(ref result);
var result = await MapToListAsync<TContract, TEntity>(entityResult.Result);
return result;
}

Expand All @@ -402,8 +397,7 @@ public async Task<ListMessageContract<TContract>> GetAllByUniqueIdentity<TEntity
var entityResult = await GetAll(queryable, query, cancellationToken);
if (!entityResult)
return entityResult.ToAnotherListContract<TContract>();
var result = await _mapperProvider.MapToListAsync<TContract>(entityResult.Result);
ValidateMappedResult(ref result);
var result = await MapToListAsync<TContract, TEntity>(entityResult.Result);
return result;
}

Expand Down Expand Up @@ -474,13 +468,11 @@ private async Task<ListMessageContract<TEntity>> InternalUpdateBulk<TEntity>(IEa
public async Task<MessageContract<TContract>> Update<TEntity, TUpdateContract, TContract>(IEasyWritableQueryableAsync<TEntity> easyWritableQueryable, TUpdateContract contract, CancellationToken cancellationToken = default)
where TEntity : class
{
var entity = await _mapperProvider.MapAsync<TEntity>(contract);
ValidateMappedResult(ref entity);
var entity = await MapAsync<TEntity, TUpdateContract>(contract);
var result = await Update(easyWritableQueryable, entity, cancellationToken);
if (!result)
return result.ToContract<TContract>();
var mappedResult = await _mapperProvider.MapAsync<TContract>(result.Result);
ValidateMappedResult(ref mappedResult);
var mappedResult = await MapAsync<TContract, TEntity>(result.Result);
return mappedResult;
}

Expand All @@ -496,8 +488,7 @@ public async Task<MessageContract<TContract>> Update<TEntity, TUpdateContract, T
public async Task<MessageContract> UpdateBulk<TEntity, TUpdateContract>(IEasyWritableQueryableAsync<TEntity> easyWritableQueryable, UpdateBulkRequestContract<TUpdateContract> request, CancellationToken cancellationToken = default)
where TEntity : class
{
var entities = await _mapperProvider.MapToListAsync<TEntity>(request.Items);
ValidateMappedResult(ref entities);
var entities = await MapToListAsync<TEntity, TUpdateContract>(request.Items);
var result = await InternalUpdateBulk(easyWritableQueryable, entities, cancellationToken, false, true);
return result;
}
Expand Down Expand Up @@ -740,8 +731,7 @@ public async Task<MessageContract> AddBulk<TEntity>(IEasyWritableQueryableAsync<
public async Task<MessageContract<TEntity>> Add<TEntity, TContract>(IEasyWritableQueryableAsync<TEntity> easyWritableQueryable, TContract contract, CancellationToken cancellationToken = default)
where TEntity : class
{
var entity = await _mapperProvider.MapAsync<TEntity>(contract);
ValidateMappedResult(ref entity);
var entity = await MapAsync<TEntity, TContract>(contract);
var result = await Add<TEntity>(easyWritableQueryable, entity, cancellationToken);
return result;
}
Expand All @@ -758,9 +748,44 @@ public async Task<MessageContract<TEntity>> Add<TEntity, TContract>(IEasyWritabl
public async Task<MessageContract> AddBulk<TEntity, TContract>(IEasyWritableQueryableAsync<TEntity> easyWritableQueryable, CreateBulkRequestContract<TContract> request, CancellationToken cancellationToken = default)
where TEntity : class
{
var entities = await _mapperProvider.MapToListAsync<TEntity>(request.Items);
ValidateMappedResult(ref entities);
var result = await AddBulk<TEntity>(easyWritableQueryable, entities, cancellationToken);
var entities = await MapToListAsync<TEntity, TContract>(request.Items);
var result = await AddBulk(easyWritableQueryable, entities, cancellationToken);
return result;
}

#endregion

#region Map

/// <summary>
///
/// </summary>
/// <typeparam name="TTo"></typeparam>
/// <typeparam name="TFrom"></typeparam>
/// <param name="items"></param>
/// <returns></returns>
protected async Task<List<TTo>> MapToListAsync<TTo, TFrom>(IEnumerable<TFrom> items)
{
if (typeof(TFrom) == typeof(TTo))
return items.Cast<TTo>().ToList();
var result = await _mapperProvider.MapToListAsync<TTo>(items);
ValidateMappedResult(ref result);
return result;
}

/// <summary>
///
/// </summary>
/// <typeparam name="TTo"></typeparam>
/// <typeparam name="TFrom"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
protected async Task<TTo> MapAsync<TTo, TFrom>(TFrom item)
{
if (typeof(TFrom) == typeof(TTo))
return (TTo)(object)item;
var result = await _mapperProvider.MapAsync<TTo>(item);
ValidateMappedResult(ref result);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using EasyMicroservices.Mapper.Interfaces;
using EasyMicroservices.ServiceContracts;
using System;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
Expand Down Expand Up @@ -112,7 +111,7 @@ public async Task<MessageContract<TResponseContract>> Add(TCreateRequestContract
var result = await Add(_easyWriteableQueryable, contract, cancellationToken);
if (!result)
return result.ToContract<TResponseContract>();
var mapped = await _mapperProvider.MapAsync<TResponseContract>(result.Result);
var mapped = await MapAsync<TResponseContract, TEntity>(result.Result);
ValidateMappedResult(ref mapped);
return mapped;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.46</Version>
<Version>0.0.0.47</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database</PackageTags>
Expand All @@ -20,7 +20,7 @@
<ItemGroup>
<PackageReference Include="EasyMicroservices.Database" Version="0.0.0.9" />
<PackageReference Include="EasyMicroservices.Mapper" Version="0.0.0.5" />
<PackageReference Include="EasyMicroservices.ServiceContracts" Version="0.0.0.17" />
<PackageReference Include="EasyMicroservices.ServiceContracts" Version="0.0.0.18" />
<PackageReference Include="EasyMicroservices.Utilities" Version="0.0.0.13" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.46</Version>
<Version>0.0.0.47</Version>
<Description>ef core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore</PackageTags>
Expand All @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Database.EntityFrameworkCore" Version="0.0.0.11" />
<PackageReference Include="EasyMicroservices.Database.EntityFrameworkCore" Version="0.0.0.12" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.46</Version>
<Version>0.0.0.47</Version>
<Description>ef core of Relational database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore,Relational</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="EasyMicroservices.Tests" Version="0.0.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.13" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit b927ebc

Please sign in to comment.