https://github.com/siberaindustries/si.unitofwork
A lightweight and flexible Repository / UnitOfWork framework.
https://github.com/siberaindustries/si.unitofwork
asp-net-core entity-framework-core object-relational-mapper orm repositories repository unitofwork
Last synced: about 1 year ago
JSON representation
A lightweight and flexible Repository / UnitOfWork framework.
- Host: GitHub
- URL: https://github.com/siberaindustries/si.unitofwork
- Owner: SiberaIndustries
- License: mit
- Created: 2020-10-01T23:03:34.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-02T23:35:34.000Z (over 3 years ago)
- Last Synced: 2024-04-27T14:32:19.869Z (about 2 years ago)
- Topics: asp-net-core, entity-framework-core, object-relational-mapper, orm, repositories, repository, unitofwork
- Language: C#
- Homepage:
- Size: 112 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SI.UnitOfWork
[](https://www.nuget.org/packages/SI.UnitOfWork)
[](https://www.nuget.org/packages/SI.UnitOfWork.EntityFrameworkCore)
[](https://github.com/SiberaIndustries/SI.UnitOfWork/actions?query=workflow%3A%22.NET+Core%22)
[](https://sonarcloud.io/dashboard?id=SiberaIndustries_SI.UnitOfWork)
[](https://sonarcloud.io/dashboard?id=SiberaIndustries_SI.UnitOfWork)
## Introduction
Most of the repository pattern libraries for .NET or .NET Core depend on the Entity Framework.
Unlike all these frameworks, the `SI.UnitOfWork` has **no such dependencies** and follows the strict paradigms of the repository pattern.
But if you want, `SI.UnitOfWork.EntityFrameworkCore` provides a complete and ready-to-use library for **SqLite**, **PostgreSQL**, **SQL Server** and the **InMemory** provider of Entity Framework.
---
## Getting Started with `SI.UnitOfWork`
### 1. Install and reference the NuGet package
```cs
Install-Package SI.UnitOfWork
```
### 2. Edit your `ConfigureServices()` method in `Startup.cs`
```cs
public void ConfigureServices(IServiceCollection services)
{
// ..
services.AddDbContext(o => o.UseInMemoryDatabase("mem-db"));
services.AddUnitOfWork();
services.AddScoped(typeof(IRepository<>), typeof(GenericRepository<>));
services.AddScoped(typeof(ICustomRepository), typeof(CustomRepository));
// ..
}
```
### 2. Inject and use
```cs
public class SampleClass
{
// Opt. 1. Inject a UoW for regular DB read and write operations
// Opt. 2. Inject a UoW-Factory in case you work with multiple databases
// Opt. 3. Inject a RepositoryFactory to get read-only repositories
public SampleClass(
IUnitOfWork unitOfWork,
IUnitOfWorkFactory unitofWorkFactory,
IRepositoryFactory repositoryFactory)
{
IRepository personRepository; // Register generic repositories manually!
ICustomRepository customRepository; // Register custom repositories manually!
// Opt. 1
personRepository = unitOfWork.GetRepository();
customRepository = unitOfWork.GetRepository();
// .. do work
unitOfWork.SaveChanges();
// Opt. 2
using var uow = unitofWorkFactory.GetUnitOfWork();
personRepository = uow.GetRepository();
customRepository = uow.GetRepository();
// .. do work
uow.SaveChanges();
// Opt. 3
personRepository = repositoryFactory.GetRepository();
customRepository = repositoryFactory.GetRepository();
// No SaveChanges available!
}
}
```
---
## Getting Started with `SI.UnitOfWork.EntityFrameworkCore`
### 1. Install and reference the NuGet package
```cs
Install-Package SI.UnitOfWork.EntityFrameworkCore
```
### 2. Edit your `ConfigureServices()` method in `Startup.cs`
```cs
public void ConfigureServices(IServiceCollection services)
{
// ..
services.AddDbContext(o => o.UseInMemoryDatabase("mem-db");
services.AddEFUnitOfWork();
services.AddScoped(typeof(ICustomRepository), typeof(CustomRepository));
// ..
}
```
### 2. Inject and use
```cs
public class SampleClass
{
public SampleClass(IUnitOfWork unitOfWork)
{
IRepository personRepository; // Generic repositories are automatically registered!
ICustomRepository customRepository; // Register custom repositories manually!
personRepository = unitOfWork.GetRepository();
customRepository = unitOfWork.GetRepository();
// .. do work
unitOfWork.SaveChanges();
}
}
```
## Open Source License Acknowledgements and Third-Party Copyrights
- Icon made by [Freepik](https://www.flaticon.com/authors/freepik)