8 Commits

Author SHA1 Message Date
84cd52cc45 feat(DbRepositoryFactory): implement IRepositoryFactory 2025-09-10 17:51:32 +02:00
272650d991 feat(IRepositoryFactory): create to capsulate repository geenration.
- update DbRepo to use IRepositoryFactory
2025-09-10 17:48:16 +02:00
7d3d3b5ae9 implement IRepository 2025-09-10 17:43:42 +02:00
453a0ccdf0 replace IEntityMapper<TEntity> with IMapper 2025-09-10 17:33:26 +02:00
0809d1215b refactor: rename Read/ReadOnly methods and mark them as obsolete
- Renamed `Read()` to `Where()` and `ReadOnly()` to `Get()` for clarity.
- Marked old `Read()` and `ReadOnly()` methods as `[Obsolete]` with guidance to use the new methods.
- Updated XML region to separate obsolete methods for better maintainability.
2025-09-10 17:30:47 +02:00
4972f05fdf refactor(repository): reorganize IRepository extensions and add missing overloads
- Added regions for Create, Update, and Delete for better structure
- Introduced `Where` extension for IRepository
- Added batch `CreateAsync` overloads for entities and DTOs
- Improved readability and consistency across IRepository extension methods
2025-09-10 17:27:33 +02:00
886a107c71 feat(repository): add extension methods for IRepository operations
- Added extensions for IRepository to support Get, Create, Update, and Delete operations.
- Maintains existing IRepository<TEntity> CreateAsync methods for DTO mapping.
- Improves consistency and usability across repository abstractions.
2025-09-10 17:22:56 +02:00
ce8e563e4e refactor(IRepository): rename Read/ReadOnly to Where/Get and mark as obsolete
- Replaced `Read()` with `Where()` and `ReadOnly()` with `Get()` in `IRepository<TEntity>`
- Marked `Read()` and `ReadOnly()` as `[Obsolete]` with guidance for the new methods
- Added non-generic `IRepository` interface with `Entity<TEntity>()` method
2025-09-10 17:04:52 +02:00
5 changed files with 106 additions and 12 deletions

View File

@@ -7,14 +7,50 @@ public static class Extensions
#region Create
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);
}
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);
}
#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
}

View File

@@ -1,18 +1,19 @@
using System.Linq.Expressions;
using AutoMapper;
using System.Linq.Expressions;
namespace DigitalData.Core.Abstraction.Application.Repository;
public interface IRepository<TEntity>
{
public IEntityMapper<TEntity> Mapper { get; }
public IMapper Mapper { get; }
public Task<TEntity> CreateAsync(TEntity entity, 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);
@@ -21,4 +22,17 @@ public interface IRepository<TEntity>
public Task DeleteAsync(Expression<Func<TEntity, bool>> expression, 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>();
}

View File

@@ -0,0 +1,6 @@
namespace DigitalData.Core.Abstraction.Application.Repository;
public interface IRepositoryFactory
{
public IRepository<TEntity> Get<TEntity>();
}

View File

@@ -1,5 +1,7 @@
using DigitalData.Core.Abstraction.Application.Repository;
using AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System.Linq.Expressions;
namespace DigitalData.Core.Infrastructure;
@@ -10,9 +12,9 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
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;
Entities = queryFactory(context);
@@ -33,9 +35,9 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
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);
@@ -65,4 +67,24 @@ public class DbRepository<TDbContext, TEntity> : IRepository<TEntity> where TDbC
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>();
}

View 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>>();
}