EzDev.GenericRepository
1.0.0-preview3.1
dotnet add package EzDev.GenericRepository --version 1.0.0-preview3.1
NuGet\Install-Package EzDev.GenericRepository -Version 1.0.0-preview3.1
<PackageReference Include="EzDev.GenericRepository" Version="1.0.0-preview3.1" />
paket add EzDev.GenericRepository --version 1.0.0-preview3.1
#r "nuget: EzDev.GenericRepository, 1.0.0-preview3.1"
// Install EzDev.GenericRepository as a Cake Addin #addin nuget:?package=EzDev.GenericRepository&version=1.0.0-preview3.1&prerelease // Install EzDev.GenericRepository as a Cake Tool #tool nuget:?package=EzDev.GenericRepository&version=1.0.0-preview3.1&prerelease
Easy Generic Repository
EzDev.GenericRepository is a very simplistic, lightweight generic repository based on EntityFramework Core, that doesn't lock you into a certain way of working. You're provided a single base class with simple CRUD-based operations, that you may override if you have other requirements.
Installation
Install with NuGet
or use .NET Core CLI
dotnet add package EzDev.GenericRepository
.
Consider using --prelease
for preview versions.
How do I get started?
Create a class that inherits from EntityRepository<T>
and implement its constructor.
In its simplest form, you can have a repository such as below.
public class SimpleEmployeeRepository : EntityRepository<Employee> {
public SimpleEmployeeRepository(DbContext context) : base(context) { }
}
That's honestly it.
The SimpleEmployeeRepository
now has default implementations for getting, adding, updating, and deleting Employee
entities.
More advanced options
Say you have a Company
type acting as an aggregate root with a list of employees, and you want to retrieve all employees whenever you query a company.
In this case, you may want to override the default Entities
property on the EntityRepository
, as demonstrated below.
public class CompanyRepository : EntityRepository<Company> {
public CompanyRepository(DbContext context) : base(context) {
Entities = context.Set<Company>().Include(c => c.Employees).AsNoTracking();
}
}
Extension and listening points
Take advantage of events to plug in your own code without having to override methods. This is great for implementing cross-cutting concerns such as logging.
You can listen to repository events in two ways: implement the methods directly in the repository, or, register them with the dependency injection framework.
public class EmployeeRepositoryWithEvents : EntityRepository<Employee> {
public EmployeeRepositoryWithEvents(DbContext context, ILogger logger) : base(context) {
Events = new RepositoryEvents<Employee> {
OnBeforeSaving = async employee => logger.LogInformation("Before saving employee {Id}", employee.Id),
OnSaved = async employee => logger.LogInformation("Saved employee {Id}", employee.Id),
OnSavingFailed = async (employee, exception) => logger.LogError("Saving employee {Id} failed with message {Message}", employee.Id, exception.Message)
};
}
}
If you don't want to pollute your repository with logging statements, then you can register the RepositoryEvents<T>
with your dependency container framework, such as below.
public class EmployeeRepositoryWithEvents : EntityRepository<Employee> {
public EmployeeRepositoryWithEvents(DbContext context, RepositoryEvents<Employee> events) :
base(context, events) { }
}
// In Startup.cs (or elsewhere)
services.AddRepository<Employee, EmployeeTestRepository>()
.WithEvents<Employee>(_ => {
OnBeforeSaving = async employee => logger.LogInformation("Before saving employee {Id}", employee.Id),
OnSaved = async employee => logger.LogInformation("Saved employee {Id}", employee.Id),
OnSavingFailed = async (employee, exception) => logger.LogError("Saving employee {Id} failed with message {Message}", employee.Id, exception.Message)
});
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
-
net6.0
- Microsoft.EntityFrameworkCore (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-preview3.1 | 694 | 12/28/2021 |
1.0.0-preview2 | 142 | 12/26/2021 |
1.0.0-preview.1 | 144 | 12/18/2021 |
0.0.0-alpha.0.15 | 157 | 12/17/2021 |
0.0.0-alpha.0.13 | 139 | 12/17/2021 |