feat(IEntityMapper): Erstellt, um Mapper zu abstrahieren.
- Integriert in IRepository und Repository
This commit is contained in:
parent
85787e7054
commit
5465fe5b49
@ -0,0 +1,34 @@
|
|||||||
|
namespace DigitalData.Core.Abstractions.Infrastructure
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Defines methods for mapping between entities and Data Transfer Objects (DTOs).
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TEntity">The type of the entity to be mapped.</typeparam>
|
||||||
|
public interface IEntityMapper<TEntity>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Maps an entity to a DTO.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDto">The type of the DTO to map to.</typeparam>
|
||||||
|
/// <param name="entity">The entity to be mapped.</param>
|
||||||
|
/// <returns>The mapped DTO.</returns>
|
||||||
|
TDto Map<TDto>(TEntity entity);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps a DTO to an entity.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
|
||||||
|
/// <param name="dto">The DTO to be mapped.</param>
|
||||||
|
/// <returns>The mapped entity.</returns>
|
||||||
|
TEntity Map<TDto>(TDto dto);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps a DTO to an existing entity.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDto">The type of the DTO to be mapped.</typeparam>
|
||||||
|
/// <param name="dto">The DTO to be mapped.</param>
|
||||||
|
/// <param name="entity">The existing entity to be updated with the mapped values.</param>
|
||||||
|
/// <returns>The updated entity.</returns>
|
||||||
|
TEntity Map<TDto>(TDto dto, TEntity entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -4,6 +4,8 @@ namespace DigitalData.Core.Abstractions.Infrastructure;
|
|||||||
|
|
||||||
public interface IRepository<TEntity>
|
public interface IRepository<TEntity>
|
||||||
{
|
{
|
||||||
|
public IEntityMapper<TEntity> Mapper { get; }
|
||||||
|
|
||||||
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken ct = default);
|
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken ct = default);
|
||||||
|
|
||||||
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken ct = default);
|
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken ct = default);
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using AutoMapper;
|
using DigitalData.Core.Abstractions.Infrastructure;
|
||||||
using DigitalData.Core.Abstractions.Infrastructure;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
@ -10,10 +9,9 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
protected internal readonly TDbContext Context;
|
protected internal readonly TDbContext Context;
|
||||||
|
|
||||||
protected internal readonly DbSet<TEntity> Entities;
|
protected internal readonly DbSet<TEntity> Entities;
|
||||||
|
public IEntityMapper<TEntity> Mapper { get; }
|
||||||
|
|
||||||
protected internal readonly IMapper Mapper;
|
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory, IEntityMapper<TEntity> mapper)
|
||||||
|
|
||||||
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory, IMapper mapper)
|
|
||||||
{
|
{
|
||||||
Context = context;
|
Context = context;
|
||||||
Entities = queryFactory(context);
|
Entities = queryFactory(context);
|
||||||
@ -36,13 +34,13 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
|
|
||||||
public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken ct = default)
|
public virtual Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var entity = Mapper.Map<TEntity>(dto);
|
var entity = Mapper.Map(dto);
|
||||||
return CreateAsync(entity, ct);
|
return CreateAsync(entity, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken ct = default)
|
public virtual Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var entities = dtos.Select(dto => Mapper.Map<TEntity>(dto));
|
var entities = dtos.Select(dto => Mapper.Map(dto));
|
||||||
return CreateAsync(entities, ct);
|
return CreateAsync(entities, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user