Skip to content

Commit

Permalink
Improvement map performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Nov 15, 2023
1 parent d4c5e65 commit 2fdffb7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 31 deletions.
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 @@ -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 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 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

0 comments on commit 2fdffb7

Please sign in to comment.