Compare commits
8 Commits
513f8c1ba3
...
84cd52cc45
| Author | SHA1 | Date | |
|---|---|---|---|
| 84cd52cc45 | |||
| 272650d991 | |||
| 7d3d3b5ae9 | |||
| 453a0ccdf0 | |||
| 0809d1215b | |||
| 4972f05fdf | |||
| 886a107c71 | |||
| ce8e563e4e |
@@ -7,14 +7,50 @@ public static class Extensions
|
|||||||
#region Create
|
#region Create
|
||||||
public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, TDto dto, CancellationToken ct = default)
|
public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, TDto dto, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var entity = repository.Mapper.Map(dto);
|
var entity = repository.Mapper.Map<TEntity>(dto);
|
||||||
return repository.CreateAsync(entity, ct);
|
return repository.CreateAsync(entity, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, IEnumerable<TDto> dtos, CancellationToken ct = default)
|
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository<TEntity> repository, IEnumerable<TDto> dtos, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var entities = dtos.Select(dto => repository.Mapper.Map(dto));
|
var entities = dtos.Select(dto => repository.Mapper.Map<TEntity>(dto));
|
||||||
return repository.CreateAsync(entities, ct);
|
return repository.CreateAsync(entities, ct);
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
|
||||||
|
#region IRepository
|
||||||
|
public static IQueryable<TEntity> Get<TEntity>(this IRepository repository) => repository.Entity<TEntity>().Get();
|
||||||
|
|
||||||
|
public static IQueryable<TEntity> Where<TEntity>(this IRepository repository) => repository.Entity<TEntity>().Where();
|
||||||
|
|
||||||
|
#region Create
|
||||||
|
public static Task<TEntity> CreateAsync<TEntity>(this IRepository repository, TEntity entity, CancellationToken cancel = default)
|
||||||
|
=> repository.Entity<TEntity>().CreateAsync(entity, cancel);
|
||||||
|
|
||||||
|
public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository repository, TDto dto, CancellationToken cancel = default)
|
||||||
|
=> repository.Entity<TEntity>().CreateAsync(dto, cancel);
|
||||||
|
|
||||||
|
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity>(this IRepository repository, IEnumerable<TEntity> entities, CancellationToken cancel = default)
|
||||||
|
=> repository.Entity<TEntity>().CreateAsync(entities, cancel);
|
||||||
|
|
||||||
|
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository repository, IEnumerable<TDto> dtos, CancellationToken cancel = default)
|
||||||
|
=> repository.Entity<TEntity>().CreateAsync(dtos, cancel);
|
||||||
|
#endregion Create
|
||||||
|
|
||||||
|
#region Update
|
||||||
|
public static Task UpdateAsync<TEntity, TDto>(this IRepository repository, TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default)
|
||||||
|
=> repository.Entity<TEntity>().UpdateAsync(dto, expression, cancel);
|
||||||
|
|
||||||
|
public static Task UpdateAsync<TEntity, TDto>(this IRepository repository, TDto dto, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
|
||||||
|
=> repository.Entity<TEntity>().UpdateAsync(dto, query, cancel);
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Delete
|
||||||
|
public static Task DeleteAsync<TEntity>(this IRepository repository, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default)
|
||||||
|
=> repository.Entity<TEntity>().DeleteAsync(expression, cancel);
|
||||||
|
|
||||||
|
public static Task DeleteAsync<TEntity>(this IRepository repository, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
|
||||||
|
=> repository.Entity<TEntity>().DeleteAsync(query, cancel);
|
||||||
|
#endregion
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
@@ -1,18 +1,19 @@
|
|||||||
using System.Linq.Expressions;
|
using AutoMapper;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
namespace DigitalData.Core.Abstraction.Application.Repository;
|
namespace DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
|
||||||
public interface IRepository<TEntity>
|
public interface IRepository<TEntity>
|
||||||
{
|
{
|
||||||
public IEntityMapper<TEntity> Mapper { get; }
|
public IMapper Mapper { get; }
|
||||||
|
|
||||||
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default);
|
public Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancel = default);
|
||||||
|
|
||||||
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default);
|
public Task<IEnumerable<TEntity>> CreateAsync(IEnumerable<TEntity> entities, CancellationToken cancel = default);
|
||||||
|
|
||||||
public IQueryable<TEntity> Read();
|
public IQueryable<TEntity> Where();
|
||||||
|
|
||||||
public IQueryable<TEntity> ReadOnly();
|
public IQueryable<TEntity> Get();
|
||||||
|
|
||||||
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
public Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
||||||
|
|
||||||
@@ -21,4 +22,17 @@ public interface IRepository<TEntity>
|
|||||||
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default);
|
||||||
|
|
||||||
public Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
|
public Task DeleteAsync(Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default);
|
||||||
|
|
||||||
|
#region Obsolete
|
||||||
|
[Obsolete("Use IRepository<TEntity>.Where")]
|
||||||
|
public IQueryable<TEntity> Read();
|
||||||
|
|
||||||
|
[Obsolete("Use IRepository<TEntity>.Get")]
|
||||||
|
public IQueryable<TEntity> ReadOnly();
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IRepository
|
||||||
|
{
|
||||||
|
public IRepository<TEntity> Entity<TEntity>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
|
||||||
|
public interface IRepositoryFactory
|
||||||
|
{
|
||||||
|
public IRepository<TEntity> Get<TEntity>();
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
using DigitalData.Core.Abstraction.Application.Repository;
|
using AutoMapper;
|
||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
namespace DigitalData.Core.Infrastructure;
|
namespace DigitalData.Core.Infrastructure;
|
||||||
@@ -10,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 IEntityMapper<TEntity> Mapper { get; }
|
public IMapper Mapper { get; }
|
||||||
|
|
||||||
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);
|
||||||
@@ -33,9 +35,9 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IQueryable<TEntity> Read() => Entities.AsQueryable();
|
public IQueryable<TEntity> Where() => Entities.AsQueryable();
|
||||||
|
|
||||||
public IQueryable<TEntity> ReadOnly() => Entities.AsNoTracking();
|
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 Task UpdateAsync<TDto>(TDto dto, Expression<Func<TEntity, bool>> expression, CancellationToken cancel = default) => UpdateAsync(dto, q => q.Where(expression), cancel);
|
||||||
|
|
||||||
@@ -65,4 +67,24 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
|
|||||||
|
|
||||||
await Context.SaveChangesAsync(cancel);
|
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>();
|
||||||
}
|
}
|
||||||
16
DigitalData.Core.Infrastructure/DbRepositoryFactory.cs
Normal file
16
DigitalData.Core.Infrastructure/DbRepositoryFactory.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace DigitalData.Core.Infrastructure;
|
||||||
|
|
||||||
|
public class DbRepositoryFactory : IRepositoryFactory
|
||||||
|
{
|
||||||
|
private readonly IServiceProvider _provider;
|
||||||
|
|
||||||
|
public DbRepositoryFactory(IServiceProvider provider)
|
||||||
|
{
|
||||||
|
_provider = provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IRepository<TEntity> Get<TEntity>() => _provider.GetRequiredService<IRepository<TEntity>>();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user