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
The main package:
PM> Install-Package SpecificatR
The abstractions containing basemodel and specification interfaces:
PM> Install-Package SpecificatR.Abstractions
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
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);
}
//Create the where clause based on Linq Expression
private static Expression<Func<Example, bool>> BuildCriteria(int id)
=> x => x.Id.Equals(id);
}
Using entities should allways inherit from the interface IBaseEntity
public Example : IBaseEntity<int>
{
public int Id {get;set;}
public string Name {get;set;}
}
- Projections: An expression to project the query to a new object. In the current version of EntityFrameworkCore, this gives issues when projecting nested entities, causing N+1 queries. This issue should be resolved in EFCore 3, therefore I put this on todo list.