refactor(DbRepository): make Mapper nullable to be able to use without adding automapper
This commit is contained in:
parent
f544ea4887
commit
4526ba189a
@ -12,9 +12,9 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
|
|
||||||
protected internal readonly DbSet<TEntity> Entities;
|
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;
|
Context = context;
|
||||||
Entities = factory.Create(context);
|
Entities = factory.Create(context);
|
||||||
@ -35,10 +35,10 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
return entities;
|
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)
|
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);
|
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--)
|
for (int i = entities.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
Mapper.Map(dto, entities[i]);
|
Mapper!.Map(dto, entities[i]);
|
||||||
Entities.Update(entities[i]);
|
Entities.Update(entities[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user