diff --git a/DigitalData.Core.Abstractions/Infrastructure/IEntityMapper.cs b/DigitalData.Core.Abstractions/Infrastructure/IEntityMapper.cs new file mode 100644 index 0000000..8f5de41 --- /dev/null +++ b/DigitalData.Core.Abstractions/Infrastructure/IEntityMapper.cs @@ -0,0 +1,34 @@ +namespace DigitalData.Core.Abstractions.Infrastructure +{ + /// + /// Defines methods for mapping between entities and Data Transfer Objects (DTOs). + /// + /// The type of the entity to be mapped. + public interface IEntityMapper + { + /// + /// Maps an entity to a DTO. + /// + /// The type of the DTO to map to. + /// The entity to be mapped. + /// The mapped DTO. + TDto Map(TEntity entity); + + /// + /// Maps a DTO to an entity. + /// + /// The type of the DTO to be mapped. + /// The DTO to be mapped. + /// The mapped entity. + TEntity Map(TDto dto); + + /// + /// Maps a DTO to an existing entity. + /// + /// The type of the DTO to be mapped. + /// The DTO to be mapped. + /// The existing entity to be updated with the mapped values. + /// The updated entity. + TEntity Map(TDto dto, TEntity entity); + } +} diff --git a/DigitalData.Core.Abstractions/Infrastructure/IRepository.cs b/DigitalData.Core.Abstractions/Infrastructure/IRepository.cs index cf94297..af5eda4 100644 --- a/DigitalData.Core.Abstractions/Infrastructure/IRepository.cs +++ b/DigitalData.Core.Abstractions/Infrastructure/IRepository.cs @@ -4,6 +4,8 @@ namespace DigitalData.Core.Abstractions.Infrastructure; public interface IRepository { + public IEntityMapper Mapper { get; } + public Task CreateAsync(TEntity entity, CancellationToken ct = default); public Task> CreateAsync(IEnumerable entities, CancellationToken ct = default); diff --git a/DigitalData.Core.Infrastructure/DbRepository.cs b/DigitalData.Core.Infrastructure/DbRepository.cs index 52acf8c..eec8ef5 100644 --- a/DigitalData.Core.Infrastructure/DbRepository.cs +++ b/DigitalData.Core.Infrastructure/DbRepository.cs @@ -1,5 +1,4 @@ -using AutoMapper; -using DigitalData.Core.Abstractions.Infrastructure; +using DigitalData.Core.Abstractions.Infrastructure; using Microsoft.EntityFrameworkCore; using System.Linq.Expressions; @@ -10,10 +9,9 @@ public class DbRepository : IRepository where TDbC protected internal readonly TDbContext Context; protected internal readonly DbSet Entities; + public IEntityMapper Mapper { get; } - protected internal readonly IMapper Mapper; - - public DbRepository(TDbContext context, Func> queryFactory, IMapper mapper) + public DbRepository(TDbContext context, Func> queryFactory, IEntityMapper mapper) { Context = context; Entities = queryFactory(context); @@ -36,13 +34,13 @@ public class DbRepository : IRepository where TDbC public virtual Task CreateAsync(TDto dto, CancellationToken ct = default) { - var entity = Mapper.Map(dto); + var entity = Mapper.Map(dto); return CreateAsync(entity, ct); } public virtual Task> CreateAsync(IEnumerable dtos, CancellationToken ct = default) { - var entities = dtos.Select(dto => Mapper.Map(dto)); + var entities = dtos.Select(dto => Mapper.Map(dto)); return CreateAsync(entities, ct); }