TekH be96bd0c07 refactor(repository): add query parameter to IRepository.Where and constrain IRepository.Entity
- Updated `IRepository<TEntity>.Where()` to accept an `Expression<Func<TEntity, bool>>` for filtering.
- Added `where TEntity : IEntity` constraint to `IRepository.Entity<TEntity>()` method.
- No functional logic changes, only interface refactoring for stronger typing and query support.
2025-09-11 18:30:38 +02:00

71 lines
3.4 KiB
C#

using System.Linq.Expressions;
namespace DigitalData.Core.Abstraction.Application.Repository;
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<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<TEntity>(dto));
return repository.CreateAsync(entities, ct);
}
#endregion
#region IRepository
public static IQueryable<TEntity> Get<TEntity>(this IRepository repository) where TEntity : IEntity
=> repository.Entity<TEntity>().Get();
public static IQueryable<TEntity> Where<TEntity>(this IRepository repository, Expression<Func<TEntity, bool>> expression)
where TEntity : IEntity
=> repository.Entity<TEntity>().Where(expression);
#region Create
public static Task<TEntity> CreateAsync<TEntity>(this IRepository repository, TEntity entity, CancellationToken cancel = default)
where TEntity : IEntity
=> repository.Entity<TEntity>().CreateAsync(entity, cancel);
public static Task<TEntity> CreateAsync<TEntity, TDto>(this IRepository repository, TDto dto, CancellationToken cancel = default)
where TEntity : IEntity
where TDto : IDto<TEntity>
=> repository.Entity<TEntity>().CreateAsync(dto, cancel);
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity>(this IRepository repository, IEnumerable<TEntity> entities, CancellationToken cancel = default)
where TEntity : IEntity
=> repository.Entity<TEntity>().CreateAsync(entities, cancel);
public static Task<IEnumerable<TEntity>> CreateAsync<TEntity, TDto>(this IRepository repository, IEnumerable<TDto> dtos, CancellationToken cancel = default)
where TEntity : IEntity
where TDto : IDto<TEntity>
=> 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)
where TEntity : IEntity
where TDto : IDto<TEntity>
=> 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)
where TEntity : IEntity
where TDto : IDto<TEntity>
=> 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)
where TEntity : IEntity
=> repository.Entity<TEntity>().DeleteAsync(expression, cancel);
public static Task DeleteAsync<TEntity>(this IRepository repository, Func<IQueryable<TEntity>, IQueryable<TEntity>> query, CancellationToken cancel = default)
where TEntity : IEntity
=> repository.Entity<TEntity>().DeleteAsync(query, cancel);
#endregion
#endregion
}