refactor(DbRepository): make Mapper nullable to be able to use without adding automapper

This commit is contained in:
tekh 2025-09-12 12:40:22 +02:00
parent f544ea4887
commit 4526ba189a

View File

@ -12,9 +12,9 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
protected internal readonly DbSet<TEntity> Entities;
public IMapper Mapper { get; }
public IMapper? Mapper { get; }
public DbRepository(TDbContext context, DbSetFactory<TDbContext, TEntity> factory, IMapper mapper)
public DbRepository(TDbContext context, DbSetFactory<TDbContext, TEntity> factory, IMapper? mapper = null)
{
Context = context;
Entities = factory.Create(context);
@ -35,10 +35,10 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
return entities;
}
public Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default) => CreateAsync(Mapper.Map<TEntity>(dto), cancel);
public Task<TEntity> CreateAsync<TDto>(TDto dto, CancellationToken cancel = default) => CreateAsync(Mapper!.Map<TEntity>(dto), cancel);
public Task<IEnumerable<TEntity>> CreateAsync<TDto>(IEnumerable<TDto> dtos, CancellationToken cancel = default)
=> CreateAsync(Mapper.Map<IEnumerable<TEntity>>(dtos), cancel);
=> CreateAsync(Mapper!.Map<IEnumerable<TEntity>>(dtos), cancel);
public IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression) => Entities.AsNoTracking().Where(expression);
@ -54,7 +54,7 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
for (int i = entities.Count - 1; i >= 0; i--)
{
Mapper.Map(dto, entities[i]);
Mapper!.Map(dto, entities[i]);
Entities.Update(entities[i]);
}