Giter Club home page Giter Club logo

specificatr's Introduction

SpecificatR

Build Status

SpecificatR contains generic repositories and interfaces for building a infrastructure. Based on EntityFramework Core and using the specification Pattern.

Supports .NET Core, .NET Standard, .NET Framework

Get it on Nuget

The main package on nuget.org:

PM> Install-Package SpecificatR

The abstractions containing basemodel and specification interfaces on nuget.org:

PM> Install-Package SpecificatR.Abstractions

Abstractions for unit testing build specifications on nuget.org:

PM> Install-Package SpecificatR.UnitTest.Abstractions

Usage

Registering dependencies

Registering the dependencies in an ASP.NET Core application, using Microsoft.Extensions.DependencyInjection, is pretty simple:

  • Install the SpecificatR package
  • Create a DbContext by standard conventions
public class ExampleContext : DbContext
    {
        public ExampleContext(DbContextOptions<ExampleContext> options)
            : base(options)
        {
        }

        public virtual DbSet<ExampleEntity> Examples { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
        }
    }
  • Call below code inside the Configure method in Startup.cs or from inside an IServiceCollection extension
services
    .AddEntityFrameworkSqlServer()
    .AddDbContext<ExampleContext>(options => options.UseSqlServer(connectionString))
    .BuildServiceProvider();

services.AddSpecificatR<ExampleContext>();

Need support for a different container? Feel free to open a new issue

Using specifications with SpecificatR

Registering the dependencies in an ASP.NET Core application, using Microsoft.Extensions.DependencyInjection, is pretty simple:

    public class ExampleSpecification : BaseSpecification<Example>
    {
        public ExampleSpecification()
            : base(BuildCriteria(id: 3))
        {
            // Use Includes
            this.AddInclude(example => example.Child);

            // Use ordering asc or desc
            this.AddOrderBy(example => example.Id, OrderByDirection.Ascending);
            this.AddOrderBy(example => example.Id, OrderByDirection.Descending);

            // Use Paging
            this.ApplyPaging(pageIndex: 1, pageSize: 20);
            
            // Ignore query filters
            this.AddIgnoreQueryFilters();
            
            // Enable EF Core tracking (Default: AsNoTracking())
            this.ApplyTracking();
        }

        //Create the where clause based on Linq Expression
        private static Expression<Func<Example, bool>> BuildCriteria(int id)
            => x => x.Id.Equals(id);
    }

Using baseEntity

Using entities should allways inherit from the interface IBaseEntity

public Example : IBaseEntity<int>
{
   public int Id {get;set;}
   public string Name {get;set;}
}

Using the repositories

Using ReadRepository and inject in any domain or business layer class.

// IReadRepository<TEntity, TIdentifier, TDbContext>
private readonly IReadRepository<TEntity: Example, TIdentifier: int, TDbContext: ExampleContext> _exampleRepository;

public ExampleClass(IReadRepository<TEntity: Example, TIdentifier: int, TDbContext: ExampleContext> exampleRepository)
{
    _exampleRepository = exampleRepository;
}

Available repository methods

IReadRepository
// Get all entities with optional tracked by EF Core. Default is set to AsNoTracking
Task<TEntity[]> GetAllAsync(bool asTracking = false);

// Get all entities based on specification (Query object).
Task<TEntity[]> GetAllAsync(ISpecification<TEntity> specification);

// Get entity with optional tracked by EF Core by Id. Default is set to AsNoTracking().
Task<TEntity> GetByIdAsync(TIdentifier id, bool asTracking = false);

// Get entity based on specification (Query object).
Task<TEntity> GetSingleWithSpecificationAsync(ISpecification<TEntity> specification);
IReadWriteRepository (All methods from IReadRepository with additional write methods below)
// Add entity to database
Task<TEntity> AddAsync(TEntity entity);

// Delete entity on database
Task DeleteByIdAsync(TIdentifier id);

// Update all properties of a entity in database
Task UpdateAsync(TEntity entity);

// Update specific properties of a entity in database
Task UpdateFieldsAsync(TEntity entity, params Expression<Func<TEntity, object>>[] properties);

specificatr's People

Contributors

cr3ature avatar huysentruitw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

specificatr's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.