View on GitHub

Generic.Repository.EFCore

This is a Generic Repository

Generic.Repository.EFCore

Code Quality - Master/Developer Codacy Badge

Travis CI - Master/Developer Build Status

Appveyor Build status

Nuget Nuget (with prereleases) Nuget

Summary

  1. Overview
  2. Version
  3. Documentation
  4. About

Overview

This project has objective to made a CRUD more easily. Adding an repository layer in application.

Principles used:

This project was build in asp.net standard 2.1.

Dependencies:

Versions

DOC

This documentation has been updated with the version 1.0.3

### 1 On startup project yor will add this

  public void ConfigureServices(IServiceCollection services)
        {
          services.AddSingleton<ICacheRepository,CacheRepository>();
          services.AddScoped(typeof(IBaseRepositoryAsync<,>), typeof(BaseRepositoryAsync<,>));
         /*...configurations...*/
        }

2 - Optional

It’s mandatory if you will use the implementation of BaseRepositoryAsync which use IFilter. With the IFilter, the query in generated automatically as the example below:

namespace Models.Filter
{*
    public c*lass CustomerFilter : IFilter
    {*
        [Fil*ter(MethodOption = LambdaMethod.Contains, MergeOption = LambdaMerge.Or)]
        [Fro*mQuery(Name = "Email")]
        publ*ic string Email { get; set; }

        [Filter(MethodOption = LambdaMethod.Contains, MergeOption = LambdaMerge.Or)]
        [FromQuery(Name = "Name")]
        public string Name { get; set; }
        
        /// Make a query using between dates
        [Filter(NameProperty = "Birthday", MethodOption = LambdaMethod.GreaterThanOrEqual, MergeOption = LambdaMerge.Or)]
        public DateTime BirthdayMax { get; set; }
        
        [Filter(NameProperty = "Birthday", MethodOption = LambdaMethod.GreaterThanOrEqual)]
        public DateTime BirthdayMin { get; set; }
    }
}

Attribue description:

| Name Attribute | Description | Attribute | Example | |—————-|————-|———–|———| | MethodOption | This say how method you will use to generate a lambda. | LambdaMethod.Contains | x => name.Contais(x.name) | | MergeOption | Tell how you will join each lambda. | MergeOption = LambdaMerge.Or | x => email.Contains(x.email) || nome.Contains(x.name) | | NameProperty | Name of property from the entity wich be referenced | |

### 3 Using the repository

    [ApiController]
    [Route("api/[controller]")]
    public class CustomerController : ControllerBase
    {
        /*....code...*/

        /*Filter example*/
        [HttpGet("filter")]
        public async Task<ActionResult<List<Customer>>> GetAllFilterAsync([FromQuery]CustomerFilter filter)
        {
            try
            {
                var list = await _repo.FilterAllAsync(filter, true);
                if (list.Count() < 1)
                    return NoContent();
                return Ok(list);
            }
            catch (Exception e)
            {
                return BadRequest($"Message: {e.Message} - StackTrace: {e.StackTrace} {(e.InnerException != null ? "InnerException" + e.InnerException : "")}");
            } 
        }

        /// Get All example
        [HttpGet]
        public async Task<ActionResult<List<Customer>>> GetAllAsync()
        {
            var list = await _repo.GetAllAsync(true);
            if (list.Count() < 1)
                return NoContent();
            return Ok(list);
        }

        /// <summary>
        /// Get All paginated
        /// </summary>
        /// <returns></returns>
        [HttpGet("page")]
        public async Task<ActionResult<Page<Customer>>> GetAllPaginated([FromQuery]PageConfig config)
        {
            try
            {
                return Ok(await _repo.GetPageAsync(config, true, token));
            }
            catch (Exception e)
            {
                return BadRequest($"Message: {e.Message} - StackTrace: {e.StackTrace} {(e.InnerException != null ? "InnerException" + e.InnerException : "")}");
            }
        }
    }

Multi transaction

    await RepositoryAsync.MultiTransactions(async ctx => {
      await RepositoryAsync.CreateAsync(value, token);
      
      await RepositoryAsync.DeleteAsync(valueToExclude, token);
      
      var page = RepositoryAsync.GetPageAsync(pageConfig, true, token);
    }, token);

Bulk transaction

    await RepositoryAsync.BulkUpdateAsync(list, ChunkSize, token);

About this project

This project born as a study project, but I have been transform in something more “professional”. Because this, that’s project still in improvement constantly. I saw this wich an way to learn new things and apliyng then more profissionaly.

If you want see some Issue please open a issue, this is a way to improve my skills and help a little the comunity around the .Net.

GitHub.Io

GitHub.Io