A repository pattern library for Entity Framework Core that provides seamless mapping between business models and data entities, with advanced expression translation capabilities.
- Two-Way Object Mapping: Automatically maps business model objects to and from data entities using AutoMapper
- Expression Translation: Translates LINQ query expressions from business model to data model, enabling queries to be written against your business layer
- Repository Pattern: Implements a clean repository pattern with
IContextRepositoryandContextRepositoryBase - Store Layer: Provides
IStoreandStoreBasefor direct DbContext operations - Advanced Querying: Supports complex operations including:
- Filtering with
Expression<Func<TModel, bool>> - Sorting and pagination
- Grouping and aggregations
- Select/expand operations
- Include navigation properties
- Filtering with
- CRUD Operations: Full support for Create, Read, Update, Delete operations
- Graph Operations: Save entire object graphs with
SaveGraphAsyncandSaveGraphsAsync - Change Tracking: Methods to manage EF Core change tracker (
AddChanges,ClearChangeTracker,DetachAllEntries) - Batch Operations: Support for batch saves and deletes
dotnet add package LogicBuilder.EntityFrameworkCore.SqlServer
//Create a context
public class SchoolContext : DbContext
{
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{
}
}
//Create Store
public interface ISchoolStore : IStore
{
}
public class SchoolStore : StoreBase, ISchoolStore
{
public SchoolStore(SchoolContext context)
: base(context)
{
}
}
//Create Repository
public interface ISchoolRepository : IContextRepository
{
}
public class SchoolRepository : ContextRepositoryBase, ISchoolRepository
{
public SchoolRepository(ISchoolStore store, IMapper mapper) : base(store, mapper)
{
}
}
//Register Services including AutoMapper profiles.
IServiceProvider serviceProvider = new ServiceCollection()
.AddDbContext<SchoolContext>
(
options =>
{
options.UseInMemoryDatabase("ContosoUniversity");
options.UseInternalServiceProvider(new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider());
}
)
.AddTransient<ISchoolStore, SchoolStore>()
.AddTransient<ISchoolRepository, SchoolRepository>()
.AddSingleton<AutoMapper.IConfigurationProvider>(new MapperConfiguration(cfg => cfg.AddProfiles(typeof(SchoolProfile).GetTypeInfo().Assembly)))
.AddTransient<IMapper>(sp => new Mapper(sp.GetRequiredService<AutoMapper.IConfigurationProvider>(), sp.GetService))
.BuildServiceProvider();
//Call the repository (inside an async method)
ISchoolRepository repository = serviceProvider.GetRequiredService<ISchoolRepository>();
ICollection<StudentModel> list = await repository.GetAsync<StudentModel, Student>();Provides methods for:
GetAsync<TModel, TData>- Retrieve entities with optional filtering and queryingCountAsync<TModel, TData>- Count entities matching a filterQueryAsync<TModel, TData, TModelReturn, TDataReturn>- Execute complex queries with expression translationSaveAsync<TModel, TData>- Save single or multiple entitiesSaveGraphAsync<TModel, TData>- Save entity graphsDeleteAsync<TModel, TData>- Delete entities by filterAddChanges<TModel, TData>- Stage changes without savingSaveChangesAsync- Persist all staged changesClearChangeTracker/DetachAllEntries- Manage change tracking
- .NET 8.0 or higher
- Entity Framework Core
- AutoMapper with expression mapping support