90 lines
3.0 KiB
C#
90 lines
3.0 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Abstraction.Application.Repository;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace DigitalData.Core.Infrastructure;
|
|
|
|
public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbContext : DbContext where TEntity : class
|
|
{
|
|
protected internal readonly TDbContext Context;
|
|
|
|
protected internal readonly DbSet<TEntity> Entities;
|
|
|
|
public IMapper Mapper { get; }
|
|
|
|
public DbRepository(TDbContext context, Func<TDbContext, DbSet<TEntity>> queryFactory, IMapper mapper)
|
|
{
|
|
Context = context;
|
|
Entities = queryFactory(context);
|
|
Mapper = mapper;
|
|
}
|
|
|
|
public virtual async Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default)
|
|
{
|
|
Entities.Add(entity);
|
|
await Context.SaveChangesAsync(cancel);
|
|
return entity;
|
|
}
|
|
|
|
public virtual async Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default)
|
|
{
|
|
Entities.AddRange(entities);
|
|
await Context.SaveChangesAsync(cancel);
|
|
return entities;
|
|
}
|
|
|
|
public IQueryable<TEntity> Where() => Entities.AsQueryable();
|
|
|
|
public IQueryable<TEntity> Get() => Entities.AsNoTracking();
|
|
|
|
public virtual Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => UpdateAsync(dto, q => q.Where(expression), cancel);
|
|
|
|
public virtual async Task UpdateAsync<TDto>(TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
|
|
{
|
|
var entities = await query(Entities).ToListAsync(cancel);
|
|
|
|
for (int i = entities.Count - 1; i >= 0; i--)
|
|
{
|
|
Mapper.Map(dto, entities[i]);
|
|
Entities.Update(entities[i]);
|
|
}
|
|
|
|
await Context.SaveChangesAsync(cancel);
|
|
}
|
|
|
|
public virtual Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => DeleteAsync(q => q.Where(expression), cancel);
|
|
|
|
public virtual async Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
|
|
{
|
|
var entities = await query(Entities).ToListAsync(cancel);
|
|
|
|
for (int i = entities.Count - 1; i >= 0; i--)
|
|
{
|
|
Entities.Remove(entities[i]);
|
|
}
|
|
|
|
await Context.SaveChangesAsync(cancel);
|
|
}
|
|
|
|
#region Obsolete
|
|
[Obsolete("Use IRepository<TEntity>.Where")]
|
|
public IQueryable<TEntity> Read() => Entities.AsQueryable();
|
|
|
|
[Obsolete("Use IRepository<TEntity>.Get")]
|
|
public IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
|
|
#endregion
|
|
}
|
|
|
|
public class DbRepository : IRepository
|
|
{
|
|
private readonly IRepositoryFactory _factory;
|
|
|
|
public DbRepository(IRepositoryFactory factory)
|
|
{
|
|
_factory = factory;
|
|
}
|
|
|
|
public IRepository<TEntity> Entity<TEntity>() => _factory.Get<TEntity>();
|
|
} |