Unverified Commit 5d8df969 authored by Almouhannad Hafez's avatar Almouhannad Hafez Committed by GitHub

Merge pull request #3 from Almouhannad/B_Add-repositories

B add repositories
parents ebcadca6 38958499
using Domain.Primitives;
namespace Domain.Repositories.Base
{
// CRUD operations
public interface IRepository<TEntity>
where TEntity : Entity
{
#region Create operation
public Task<TEntity> CreateAsync(TEntity entity);
#endregion
#region Read operations
public Task<TEntity?> GetByIdAsync(int id);
public Task<ICollection<TEntity>> GetAllAsync();
#endregion
#region Update oprtation
public Task<TEntity> UpdateAsync(TEntity entity);
#endregion
#region Delete operation
public Task DeleteAsync(int id);
#endregion
}
}
using Domain.Primitives;
using Domain.Repositories.Base;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
namespace Persistence.Repositories.Base;
public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
{
#region Ctor DI for context
private readonly ClinicsDbContext _context;
public Repositroy(ClinicsDbContext context)
{
_context = context;
}
#endregion
#region Apply specification
#endregion
#region CRUD
#region Create operation
public async Task<TEntity> CreateAsync(TEntity entity)
{
return await _context.Set<TEntity>().FirstAsync(e => e.Id == entity.Id);
}
#endregion
#region Read operations
public async Task<TEntity?> GetByIdAsync(int id)
{
return await _context.Set<TEntity>().FindAsync(id);
}
public async Task<ICollection<TEntity>> GetAllAsync()
{
return await _context.Set<TEntity>().ToListAsync<TEntity>();
}
#endregion
#region Update operation
public async Task<TEntity> UpdateAsync(TEntity entity)
{
var query = _context.Set<TEntity>().Update(entity);
await _context.SaveChangesAsync();
return query.Entity;
}
#endregion
#region Delete operation
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
if (entity is not null)
_context.Set<TEntity>().Remove(entity);
await _context.SaveChangesAsync();
}
#endregion
#endregion
}
using Domain.Primitives;
using System.Linq.Expressions;
namespace Persistence.Specifications.Base;
public abstract class Specification<TEntity> where TEntity : Entity
{
protected Specification(Expression<Func<TEntity, bool>>? criteria)
{
Criteria = criteria;
}
#region Where criteria
public Expression<Func<TEntity, bool>>? Criteria { get; }
#endregion
#region Includes
public List<Expression<Func<TEntity, object>>> IncludeExpressions { get; } = new();
#endregion
#region Order by
public Expression<Func<TEntity, object>>? OrderByExpression { get; private set; }
public Expression<Func<TEntity, object>>? OrderByDescendingExpression { get; private set; }
#endregion
#region Add methods
protected void AddInclude(Expression<Func<TEntity, object>> includeExpression)
{
IncludeExpressions.Add(includeExpression);
}
protected void AddOrderBy(Expression<Func<TEntity, object>> orderByExpression)
{
OrderByExpression = orderByExpression;
}
protected void AddOrderByDescending(Expression<Func<TEntity, object>> orderByDescendingExpression)
{
OrderByDescendingExpression = orderByDescendingExpression;
}
#endregion
}
using Domain.Primitives;
using Microsoft.EntityFrameworkCore;
using Persistence.Specifications.Base;
namespace Persistence.Specifications.Evaluator;
public static class SpecificationEvaluator
{
public static IQueryable<TEntity> GetQuery<TEntity>(
IQueryable<TEntity> inputQueryable,
Specification<TEntity> specification)
where TEntity : Entity
{
IQueryable<TEntity> queryable = inputQueryable;
if (specification.Criteria is not null)
{
queryable = queryable.Where(specification.Criteria);
}
specification.IncludeExpressions.Aggregate(
queryable,
(current, includeExpression) =>
current.Include(includeExpression)
);
if (specification.OrderByExpression is not null)
{
queryable = queryable.OrderBy(specification.OrderByExpression);
}
if (specification.OrderByDescendingExpression is not null)
{
queryable = queryable.OrderByDescending(specification.OrderByDescendingExpression);
}
return queryable;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment