feat(Repository): Add and implement ReadOrDefaultAsync method.

- add read method can map for each
This commit is contained in:
Developer 02
2025-04-22 23:04:11 +02:00
parent 304f5b7b4c
commit 72f735272f
8 changed files with 55 additions and 15 deletions

View File

@@ -17,9 +17,9 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackAsTool>False</PackAsTool>
<PackageIcon>core_icon.png</PackageIcon>
<Version>3.4.1</Version>
<AssemblyVersion>3.4.1</AssemblyVersion>
<FileVersion>3.4.1</FileVersion>
<Version>3.4.2</Version>
<AssemblyVersion>3.4.2</AssemblyVersion>
<FileVersion>3.4.2</FileVersion>
</PropertyGroup>
<ItemGroup>

View File

@@ -20,15 +20,15 @@ public static class Extensions
#region Read
public static async Task<TEntity?> ReadFirstOrDefaultAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
=> (await repository.ReadAsync(expression)).FirstOrDefault();
=> (await repository.ReadAllAsync(expression)).FirstOrDefault();
public static async Task<TEntity> ReadFirstAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
=> (await repository.ReadAsync(expression)).First();
=> (await repository.ReadAllAsync(expression)).First();
public static async Task<TEntity?> ReadSingleOrDefaultAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
=> (await repository.ReadAsync(expression)).SingleOrDefault();
=> (await repository.ReadAllAsync(expression)).SingleOrDefault();
public static async Task<TEntity> ReadSingleAsync<TEntity>(this IRepository<TEntity> repository, Expression<Func<TEntity, bool>>? expression = null)
=> (await repository.ReadAsync(expression)).Single();
=> (await repository.ReadAllAsync(expression)).Single();
#endregion
}

View File

@@ -14,6 +14,14 @@
/// <returns>The mapped DTO.</returns>
TDto Map<TDto>(TEntity entity);
/// <summary>
/// Maps an entity list to a DTO list.
/// </summary>
/// <typeparam name="TDto">The type of the DTO to map to.</typeparam>
/// <param name="entities">The entity list to be mapped.</param>
/// <returns>The mapped DTO list.</returns>
IEnumerable<TDto> Map<TDto>(IEnumerable<TEntity> entities);
/// <summary>
/// Maps a DTO to an entity.
/// </summary>
@@ -22,6 +30,14 @@
/// <returns>The mapped entity.</returns>
TEntity Map<TDto>(TDto dto);
/// <summary>
/// Maps a DTO list to an entity list.
/// </summary>
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
/// <param name="dtos">The DTO list to be mapped.</param>
/// <returns>The mapped entity list.</returns>
IEnumerable<TEntity> Map<TDto>(IEnumerable<TDto> dtos);
/// <summary>
/// Maps a DTO to an existing entity.
/// </summary>

View File

@@ -10,7 +10,13 @@ public interface IRepository<TEntity>
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken ct = default);
public Task<IEnumerable<TEntity>> ReadAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
public Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
public Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default);
public Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default);
public Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default);
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default);

View File

@@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>DigitalData.Core.Infrastructure.AutoMapper</PackageId>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company>
<Product>DigitalData.Core.Infrastructure.AutoMapper</Product>
@@ -16,8 +16,8 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<RepositoryType>digital data core abstractions clean architecture mapping</RepositoryType>
<PackageTags>digital data core infrastructure clean architecture mapping</PackageTags>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<AssemblyVersion>1.0.1</AssemblyVersion>
<FileVersion>1.0.1</FileVersion>
</PropertyGroup>
<ItemGroup>

View File

@@ -14,7 +14,11 @@ public class EntityAutoMapper<TEntity> : IEntityMapper<TEntity>
public TDto Map<TDto>(TEntity entity) => _rootMapper.Map<TDto>(entity);
public IEnumerable<TDto> Map<TDto>(IEnumerable<TEntity> entities) => _rootMapper.Map<IEnumerable<TDto>>(entities);
public TEntity Map<TDto>(TDto dto) => _rootMapper.Map<TEntity>(dto);
public IEnumerable<TEntity> Map<TDto>(IEnumerable<TDto> dtos) => _rootMapper.Map<IEnumerable<TEntity>>(dtos);
public TEntity Map<TDto>(TDto dto, TEntity entity) => _rootMapper.Map(dto, entity);
}

View File

@@ -33,11 +33,25 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
return entities;
}
public virtual async Task<IEnumerable<TEntity>> ReadAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
public virtual async Task<IEnumerable<TEntity>> ReadAllAsync(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
=> expression is null
? await Entities.AsNoTracking().ToListAsync(ct)
: await Entities.AsNoTracking().Where(expression).ToListAsync(ct);
public virtual async Task<TEntity?> ReadOrDefaultAsync(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
=> single
? await Entities.AsNoTracking().Where(expression).SingleOrDefaultAsync(ct)
: await Entities.AsNoTracking().Where(expression).FirstOrDefaultAsync(ct);
public virtual async Task<IEnumerable<TDto>> ReadAllAsync<TDto>(Expression<Func<TEntity, bool>>? expression = null, CancellationToken ct = default)
=> Mapper.Map<TDto>(await ReadAllAsync(expression, ct));
public virtual async Task<TDto?> ReadOrDefaultAsync<TDto>(Expression<Func<TEntity, bool>> expression, bool single = true, CancellationToken ct = default)
{
var entity = await ReadOrDefaultAsync(expression, single, ct);
return entity is null ? default : Mapper.Map<TDto>(entity);
}
public virtual async Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken ct = default)
{
var entities = await Entities.Where(expression).ToListAsync(ct);

View File

@@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>DigitalData.Core.Infrastructure</PackageId>
<Version>2.0.2</Version>
<Version>2.0.3</Version>
<Authors>Digital Data GmbH</Authors>
<Company>Digital Data GmbH</Company>
<Product>DigitalData.Core.Infrastructure</Product>
@@ -16,8 +16,8 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<RepositoryType>digital data core abstractions clean architecture</RepositoryType>
<PackageTags>digital data core infrastructure clean architecture</PackageTags>
<AssemblyVersion>2.0.2</AssemblyVersion>
<FileVersion>2.0.2</FileVersion>
<AssemblyVersion>2.0.3</AssemblyVersion>
<FileVersion>2.0.3</FileVersion>
</PropertyGroup>
<ItemGroup>