QDev.CSharp.EFCore.RepositoryPattern
1.0.0
See the version list below for details.
dotnet add package QDev.CSharp.EFCore.RepositoryPattern --version 1.0.0
NuGet\Install-Package QDev.CSharp.EFCore.RepositoryPattern -Version 1.0.0
<PackageReference Include="QDev.CSharp.EFCore.RepositoryPattern" Version="1.0.0" />
paket add QDev.CSharp.EFCore.RepositoryPattern --version 1.0.0
#r "nuget: QDev.CSharp.EFCore.RepositoryPattern, 1.0.0"
// Install QDev.CSharp.EFCore.RepositoryPattern as a Cake Addin #addin nuget:?package=QDev.CSharp.EFCore.RepositoryPattern&version=1.0.0 // Install QDev.CSharp.EFCore.RepositoryPattern as a Cake Tool #tool nuget:?package=QDev.CSharp.EFCore.RepositoryPattern&version=1.0.0
Database Manager EFCore Repository Pattern
A library for implementing generic repositories and unit of work with Entity Framework Core. This implementation uses a single instance of the DbContext for all repositories to avoid concurrency issues.
Table of Contents
- Installation
- Creating the DbContext
- Injecting the Repository Pattern
- Creating the Service
- IUnitOfWork methods
- IRepository methods
Installation
Using the NuGet package manager console within Visual Studio run the following command:
Install-Package QDev.CSharp.EFCore.RepositoryPattern
Or using the .NET Core CLI from a terminal window:
dotnet add package QDev.CSharp.EFCore.RepositoryPattern
Creating the DbContext
Create your DbContext inheriting from Microsoft.EntityFrameworkCore.DbContext
:
public class MyDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
base.OnModelCreating(modelBuilder);
}
}
Configure your DbContext in the Program
class:
builder.Services.AddDbContext<DbContext, SuinQShopModuleDbContext>(opt =>
{
opt.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionString"));
});
Injecting the RepositoryPattern's Services
Add the RepositoryPattern
services to the Service Collection:
builder.Services.AddRepositoryPattern();
Creating the Service
To keep it simple, consider using the Service<TEntity>
form. This approach allows you to create a generic Service<TEntity>
class where you implement fundamental CRUD functionalities, reducing code repetition. As a result, specific services for each table can inherit CRUD functionalities from Service<TEntity>
and focus solely on their unique implementations.
Here's an example of how you should implement the Service class:
public class MyService
{
private readonly IUnitOfWork _unitOfWork;
public MyService(IServiceProvider services)
{
var scoped = provider.CreateScope();
_unitOfWork = scoped.ServiceProvider.GetRequiredService<IUnitOfWork>();
}
public async Task<MyEntity> CreateMyEntity(MyEntity entity, CancellationToken cancellationToke = default)
{
var newEntity await _unitOfWork.Repository<TEntity>().InsertOneAsync(entity, cancellationToken);
await _unitOfWork.SaveAsync(cancellationToken);
return newEntity;
}
// and more methods as needed
}
UnitOfWork
Key Aspects
IUnitOfWork
represents a unit of work for coordinating and managing database operations using Entity Framework Core.- It serves as a central hub for working with the database context, repositories, and transactions.
Functions
Function | Description |
---|---|
IRepository<TEntity> Repository<TEntity>() |
Retrieves a repository for managing entities of the specified type. |
void BeginTransaction() |
Initiates a new database transaction. |
bool SupportsSavePoints |
Determines whether the database provider supports savepoints. |
Task BeginTransactionAsync(CancellationToken cancellationToken = default) |
Asynchronously initiates a new database transaction. |
void RollBack() |
Rolls back the current transaction. |
Task RollBackAsync(CancellationToken cancellationToken = default) |
Asynchronously rolls back the current transaction. |
void RollBackToSavePoint(string name) |
Rolls back the transaction to a specific savepoint. |
Task RollBackToSavePointAsync(string name, CancellationToken cancellationToken = default) |
Asynchronously rolls back the transaction to a specific savepoint. |
void Commit() |
Commits the current transaction. |
Task CommitAsync(CancellationToken cancellationToken = default) |
Asynchronously commits the current transaction. |
void CreateSavePoint(string name) |
Creates a savepoint within the current transaction. |
Task CreateSavePointAsync(string name, CancellationToken cancellationToken = default) |
Asynchronously creates a savepoint within the current transaction. |
void ReleaseSavePoint(string name) |
Releases a previously created savepoint within the current transaction. |
Task ReleaseSavePointAsync(string name, CancellationToken cancellationToken = default) |
Asynchronously releases a previously created savepoint within the current transaction. |
Task<int> SaveAsync(CancellationToken cancellationToken = default) |
Asynchronously saves changes to the database. |
int Save() |
Synchronously saves changes to the database. Returns the number of entities affected by the save operation. |
Repository methods
Here's the markdown documentation for the second interface:
Description
IRepository<TEntity>
represents a repository for managing entities of type TEntity
within an Entity Framework Core context.
Type Parameter
TEntity
: The type of entities managed by this repository.
Functions
Function | Description |
---|---|
Task<IQueryable<TEntity>> GetManyAsync(Expression<Func<TEntity, bool>>? filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, List<string>? includeProperties = null, CancellationToken cancellationToken = default) |
Asynchronously retrieves multiple entities from the repository based on filter criteria. |
Task<TEntity?> GetOneAsync(Expression<Func<TEntity, bool>> filter, List<string>? includeProperties = null, CancellationToken cancellationToken = default) |
Asynchronously retrieves a single entity from the repository based on filter criteria. |
Task<TEntity> InsertOneAsync(TEntity entity, CancellationToken cancellationToken = default) |
Asynchronously inserts a single entity into the repository. |
Task InsertManyAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default) |
Asynchronously inserts multiple entities into the repository. |
Task<TEntity> UpdateOneAsync(TEntity entity, CancellationToken cancellationToken) |
Asynchronously updates a single entity in the repository. |
Task UpdateManyAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken) |
Asynchronously updates multiple entities in the repository. |
Task<TEntity> RemoveOneAsync(TEntity entity, CancellationToken cancellationToken = default) |
Asynchronously removes a single entity from the repository. |
Task<TEntity> RemoveOneAsync(Expression<Func<TEntity, bool>> filter, CancellationToken cancellationToken = default) |
Asynchronously removes a single entity from the repository based on filter criteria. |
Task RemoveManyAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default) |
Asynchronously removes multiple entities from the repository. |
Task RemoveManyAsync(Expression<Func<TEntity, bool>> filter, CancellationToken cancellationToken = default) |
Asynchronously removes multiple entities from the repository based on filter criteria. |
int Save() |
Synchronously saves changes to the repository. Returns the number of entities affected by the save operation. |
Task<int> SaveAsync(CancellationToken cancellationToken) |
Asynchronously saves changes to the repository. Returns the number of entities affected by the save operation. |
Please note that some functions include optional parameters, and there are exception descriptions for error handling.
Type Parameters
TEntity
: The type of entities managed by this repository.
Product | Versions 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. |
-
net7.0
- Microsoft.EntityFrameworkCore (>= 7.0.12)
- Microsoft.EntityFrameworkCore.Relational (>= 7.0.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
frist release