Gleeman.Repository.EFCore 7.0.0

dotnet add package Gleeman.Repository.EFCore --version 7.0.0
NuGet\Install-Package Gleeman.Repository.EFCore -Version 7.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Gleeman.Repository.EFCore" Version="7.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gleeman.Repository.EFCore --version 7.0.0
#r "nuget: Gleeman.Repository.EFCore, 7.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Gleeman.Repository.EFCore as a Cake Addin
#addin nuget:?package=Gleeman.Repository.EFCore&version=7.0.0

// Install Gleeman.Repository.EFCore as a Cake Tool
#tool nuget:?package=Gleeman.Repository.EFCore&version=7.0.0

GleemanRepository

Command Repositories

Create
public interface IEFCreateAsyncRepository<TEntity> : IEFAsyncRepository
where TEntity : class
{
    Task<TEntity> CreateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
    Task InsertAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
    Task<IEnumerable<TEntity>> CreateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default(CancellationToken));
    Task InsertRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default(CancellationToken));
}
public interface IEFCreateSyncRepository<TEntity> : IEFSyncRepository
where TEntity : class
{
    TEntity Create(TEntity entity);
    void Insert(TEntity entity);
    IEnumerable<TEntity> CreateRange(IEnumerable<TEntity> entities);
    void InsertRange(IEnumerable<TEntity> entities);
}
Delete
public interface IEFDeleteAsyncRepository<TEntity>:IEFAsyncRepository
where TEntity : class
{
    Task<TEntity> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
    Task RemoveAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
    Task<IEnumerable<TEntity>> DeleteRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default(CancellationToken));
    Task RemoveRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default(CancellationToken));
}
public interface IEFDeleteSyncRepository<TEntity>:IEFSyncRepository
 where TEntity : class
{
    TEntity Delete(TEntity entity);
    void Remove(TEntity entity);
    IEnumerable<TEntity> DeleteRange(IEnumerable<TEntity> entities);
    void RemoveRange(IEnumerable<TEntity> entities);
}
Update
public interface IEFUpdateAsyncRepository<TEntity>:IEFAsyncRepository
where TEntity : class
{
    Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default);
    Task EditAsync(TEntity entity, CancellationToken cancellationToken = default);
    Task<IEnumerable<TEntity>> UpdateRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
    Task EditRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
}
public interface IEFUpdateSyncRepository<TEntity>:IEFSyncRepository
where TEntity : class
{
    TEntity Update(TEntity entity);
    void Edit(TEntity entity);
    IEnumerable<TEntity> UpdateRange(IEnumerable<TEntity> entities);
    void EditRange(IEnumerable<TEntity> entities);
}

Query Repositories

public interface IEFQueryAsyncRepository<TEntity>
where TEntity : class
{
    Task<IQueryable<TEntity>> QueryAsync(bool noTracking,
        Action<Pagination> pagination = null,
        Expression<Func<TEntity, bool>> filter = null,
        CancellationToken cancellationToken = default(CancellationToken),
        params Expression<Func<TEntity, object>>[] includeProperties);

    Task<IEnumerable<TEntity>> ReadAllAsync(bool noTracking,
        Action<Pagination> pagination = null,
        Expression<Func<TEntity, bool>> filter = null,
        CancellationToken cancellationToken = default(CancellationToken),
        params Expression<Func<TEntity, object>>[] includeProperties);

    Task<TEntity> ReadSingleOrDefaultAsync(bool noTracking,Expression<Func<TEntity, bool>> filter,
        CancellationToken cancellationToken = default(CancellationToken),
        params Expression<Func<TEntity, object>>[] includeProperties);

    Task<TEntity> ReadFirstOrDefaultAsync(bool noTracking,Expression<Func<TEntity, bool>> filter,
        CancellationToken cancellationToken = default(CancellationToken),
        params Expression<Func<TEntity, object>>[] includeProperties);

    Task<TEntity> ReadLastOrDefaultAsync(bool noTracking, Expression<Func<TEntity, bool>> filter,
       CancellationToken cancellationToken = default(CancellationToken),
       params Expression<Func<TEntity, object>>[] includeProperties);

    Task<TEntity> ReadFirstAsync(bool noTracking,Expression<Func<TEntity, bool>> filter,
        CancellationToken cancellationToken = default(CancellationToken),
        params Expression<Func<TEntity, object>>[] includeProperties);

    Task<TEntity> ReadLastAsync(bool noTracking,Expression<Func<TEntity, bool>> filter,
        CancellationToken cancellationToken = default(CancellationToken),
        params Expression<Func<TEntity, object>>[] includeProperties);

    Task<TEntity> ReadSingleAsync(bool noTracking,Expression<Func<TEntity, bool>> filter,
        CancellationToken cancellationToken = default(CancellationToken),
        params Expression<Func<TEntity, object>>[] includeProperties);

    Task<bool> ExistAsync(Expression<Func<TEntity, bool>> filter, CancellationToken cancellationToken = default(CancellationToken));
}
public interface IEFQuerySyncRepository<TEntity>
where TEntity : class
{
    IQueryable<TEntity> Query(bool noTracking,
        Action<Pagination> pagination = null,
        Expression<Func<TEntity, bool>> filter = null,
        params Expression<Func<TEntity, object>>[] includeProperties);

    IEnumerable<TEntity> ReadAll(bool noTracking,
         Action<Pagination> pagination = null,
        Expression<Func<TEntity, bool>> filter = null,
        params Expression<Func<TEntity, object>>[] includeProperties);

    TEntity ReadSingleOrDefault(bool noTracking,
        Expression<Func<TEntity, bool>> filter,
        params Expression<Func<TEntity, object>>[] includeProperties);

    TEntity ReadFirstOrDefault(bool noTracking,
        Expression<Func<TEntity, bool>> filter,
        params Expression<Func<TEntity, object>>[] includeProperties);

    TEntity ReadLastOrDefault(bool noTracking,
        Expression<Func<TEntity, bool>> filter,
       params Expression<Func<TEntity, object>>[] includeProperties);

    TEntity ReadFirst(bool noTracking,
        Expression<Func<TEntity, bool>> filter,
        params Expression<Func<TEntity, object>>[] includeProperties);

    TEntity ReadLast(bool noTracking,
        Expression<Func<TEntity, bool>> filter,
        params Expression<Func<TEntity, object>>[] includeProperties);

    TEntity ReadSingle(bool noTracking,
        Expression<Func<TEntity, bool>> filter,
        params Expression<Func<TEntity, object>>[] includeProperties);

    bool Exist(Expression<Func<TEntity, bool>> filter);
}

EXAMPLE

public interface IProductQueryRepository:IEFQueryAsyncRepository<Product>,IEFQuerySyncRepository<Product>
{
}

public interface IProductCommandRepository : 
IEFCreateAsyncRepository<Product>, 
IEFCreateSyncRepository<Product>, 
IEFUpdateAsyncRepository<Product>, 
IEFUpdateSyncRepository<Product>, 
IEFDeleteAsyncRepository<Product>,
IEFDeleteSyncRepository<Product>
{
    
}

public class ProductQueryRepository : EFQueryRepository<Product, AppDbContext>, IProductQueryRepository
{
    public ProductQueryRepository(AppDbContext dbContext) : base(dbContext)
    {
    }
}

public class ProductCommandRepository : EFCommandRepository<Product, AppDbContext>,IProductCommandRepository
{
    public ProductCommandRepository(AppDbContext dbContext) : base(dbContext)
    {
    }
}
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductsController : ControllerBase
    {
        private readonly IProductCommandRepository _command;
        private readonly IProductQueryRepository _query;

        public ProductsController(IProductCommandRepository command, IProductQueryRepository query)
        {
            _command = command;
            _query = query;
        }


        // Async Queries
        
        [HttpGet]
        public async Task<IActionResult> GetProductsAsync()
        {
            var result = await _query.ReadAllAsync(noTracking: true);
            return Ok(result);
        }

        [HttpGet]
        public async Task<IActionResult> GetProductsWithCategoryAsync()
        {
            var result = await _query.ReadAllAsync(noTracking: true, includeProperties: x => x.Category);
            return Ok(result);
        }

        [HttpGet]
        public async Task<IActionResult> GetProductsWithPaginationAsync([FromQuery] int pageNumber, [FromQuery] int pageSize)
        {
            var result = await _query.ReadAllAsync(noTracking: true, pagination: pagination =>
            {

                pagination.MaxPageSize = 50;
                pagination.PageNumber = pageNumber;
                pagination.PageSize = pageSize;
            });
            return Ok(result);
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetProductAsync(int id)
        {
            var result = await _query.ReadSingleOrDefaultAsync(true, x => x.Id == id);
            return Ok(result);
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> GetProductWithCategoryAsync(int id)
        {
            var result = await _query.ReadSingleOrDefaultAsync(true, x => x.Id == id, includeProperties: x => x.Category);
            return Ok(result);
        }
        

        // Sync Queries

        [HttpGet]
        public IActionResult GetProducts()
        {
            var result = _query.ReadAll(noTracking: true);
            return Ok(result);
        }

        [HttpGet]
        public IActionResult GetProductsWithCategory()
        {
            var result = _query.ReadAll(noTracking: true, includeProperties: x => x.Category);
            return Ok(result);
        }

        [HttpGet]
        public IActionResult GetProductsWithPagination([FromQuery] int pageNumber, [FromQuery] int pageSize)
        {
            var result = _query.ReadAll(noTracking: true, pagination: pagination =>
            {

                pagination.MaxPageSize = 50;
                pagination.PageNumber = pageNumber;
                pagination.PageSize = pageSize;
            });
            return Ok(result);
        }

        [HttpGet("{id}")]
        public IActionResult GetProduct(int id)
        {
            var result = _query.ReadSingleOrDefault(true, x => x.Id == id);
            if(result == null) return NotFound();
            return Ok(result);
        }

        [HttpGet("{id}")]
        public IActionResult GetProductWithCategory(int id)
        {
            var result = _query.ReadSingleOrDefault(true, x => x.Id == id, includeProperties: x => x.Category);
            if(result == null) return NotFound();
            return Ok(result);
        }


        // Async Commands


        [HttpPost]
        public async Task<IActionResult> CreateAsync([FromBody] ProductDto createProduct)
        {
            Product product = new()
            {
                DisplayName = createProduct.DisplayName,
                Price = createProduct.Price,
                CategoryId = createProduct.CategoryId,
            };

            var result = await _command.CreateAsync(product);

            await _command.ExecuteAsync();
            return Created("", result);

        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateAsync(int id, [FromBody] ProductDto updateProduct)
        {
            var product = await _query.ReadFirstOrDefaultAsync(true, x => x.Id == id);
            if (product is null) return NotFound();

            product.DisplayName = updateProduct.DisplayName;
            product.Price = updateProduct.Price;
            product.CategoryId = updateProduct.CategoryId;

            await _command.EditAsync(product);
            await _command.ExecuteAsync();
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteAsync(int id)
        {
            IQueryable<Product> query = await _query.QueryAsync(true, filter: x => x.Id == id);
            Product product = query.SingleOrDefault();
            if (product == null) return NotFound();

            await _command.RemoveAsync(product);
            await _command.ExecuteAsync();
            return NoContent();
        }


        // Sync Commands

        [HttpPost]
        public IActionResult Create([FromBody] ProductDto createProduct)
        {
            Product product = new()
            {
                DisplayName = createProduct.DisplayName,
                Price = createProduct.Price,
                CategoryId = createProduct.CategoryId,
            };

            var result = _command.Create(product);
            _command.Execute();
            return Created("", result);

        }

        [HttpPut("{id}")]
        public IActionResult Update(int id, [FromBody] ProductDto updateProduct)
        {
            var product = _query.ReadFirstOrDefault(true, x => x.Id == id);
            if (product is null) return NotFound();

            product.DisplayName = updateProduct.DisplayName;
            product.Price = updateProduct.Price;
            product.CategoryId = updateProduct.CategoryId;

            _command.Edit(product);
            _command.Execute();
            return NoContent();
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            IQueryable<Product> query = _query.Query(true, filter: x => x.Id == id);
            Product product = query.SingleOrDefault();
            if (product == null) return NotFound();

            _command.Remove(product);
            _command.Execute();
            return NoContent();
        }
    }
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.0.0 226 10/20/2023
6.0.0 111 10/20/2023