π Back To All NuGet Packages
About
A lightweight and ready-made implementation of unit of work pattern + NoSQL database.
Twileloop.UOW is a package that ships a plug and play model predefined repository, unit of work pattern on top of 2 popular NoSQL databases. There are 2 varients of Twileloop.UOW for LiteDB and MongoDB support
License
Twileloop.UOW.LiteDB & Twileloop.UOW.MongoDB - are licensed under the MIT License. See the LICENSE file for more details.
This library is absolutely free. If it gives you a smile, A small coffee would be a great way to support my work. Thank you for considering it!
Usage
To get started, You have to select which package to install:
- If you prefer to use file-based database, Install Twileloop.UOW with LiteDB support. Install
Twileloop.UOW.LiteDB
package - If you prefer to use a centrally deployed MongoDB database, Install Twileloop.UOW with MongoDB support. Install
Twileloop.UOW.MongoDB
package
2. Install Package
Choose the installation that suites your need
Driver | To Use | Install Package |
---|---|---|
![]() |
LiteDB | dotnet add package Twileloop.UOW.LiteDB |
![]() |
MongoDB | dotnet add package Twileloop.UOW.MongoDB |
Supported Features
Feature | LiteDB | MongoDB |
---|---|---|
Create | β | β |
Read | β | β |
Update | β | β |
Delete | β | β |
Full Repository Access | β | β |
Multiple Databases | β | β |
Database Level Transactions | β | β |
β - Available Β Β Β π§ - Work In Progress Β Β Β β - Not Available
1. Register all databases (ASP.NET dependency injection)
(For APIs with scopped injection)
//LiteDB
builder.Services.AddUnitOfWork((uow) => {
uow.Connections = new List<LiteDBConnection>
{
new LiteDBConnection("DatabaseA", "Filename=DatabaseA.db; Connection=Shared; Password=****;"),
new LiteDBConnection("DatabaseB", "Filename=DatabaseB.db; Connection=Shared; Password=****;")
};
});
(For console apps, worker services etc..)
//LiteDB
builder.Services.AddSingletonUnitOfWork((uow) => {
uow.Connections = new List<LiteDBConnection>
{
new LiteDBConnection("DatabaseA", "Filename=DatabaseA.db; Connection=Shared; Password=****;"),
new LiteDBConnection("DatabaseB", "Filename=DatabaseB.db; Connection=Shared; Password=****;")
};
});
(Mongo support for UOW is always injected singleton)
//MongoDB
builder.Services.AddUnitOfWork((uow) => {
uow.Connections = new List<MongoDBConnection>
{
new MongoDBConnection("DatabaseA", "mongodb+srv://Uername:****@Cluster"),
new MongoDBConnection("DatabaseB", "mongodb+srv://Uername:****@Cluster")
};
});
2. For Non Dependency Injection Setup (Like Console apps)
//LiteDB
var context = LiteDB.Support.Extensions.BuildDbContext(option =>
{
option.Connections = new List<LiteDBConnection>
{
new LiteDBConnection("DatabaseA", "Filename=DatabaseA.db; Connection=Shared; Password=****;"),
new LiteDBConnection("DatabaseB", "Filename=DatabaseB.db; Connection=Shared; Password=****;")
};
});
var uow = new LiteDB.Core.UnitOfWork(context);
//MongoDB
var context = MongoDB.Support.Extensions.BuildDbContext(option =>
{
option.Connections = new List<MongoDBConnection>
{
new MongoDBConnection("DatabaseA", "mongodb+srv://Username:****@Cluster"),
new MongoDBConnection("DatabaseB", "mongodb+srv://Username:****@Cluster")
};
});
var uow = new MongoDB.Core.UnitOfWork(context);
PLEASE NOTE
β - BSON Serialization will work only on serializable properties. Objects like DataTable
etc.. are non-generic which canβt be stored as in DB
3. DB Models
Ensure your DB models inherit from EntityBase
for support
NEVER USE A PROPERTY CALLED βIdβ IN YOUR MODEL SINCE A DEFAULT IDENTITY COLUMN WITH NAME βIdβ WILL INHERIT FROM
EntityBase
class
public class Dogs : EntityBase
{
public Guid NickName { get; set; }
public string Name { get; set; }
}
4. Inject and Use as required
[ApiController]
public class HomeController : ControllerBase
{
private readonly UnitOfWork uow;
public HomeController(UnitOfWork uow)
{
this.uow = uow;
}
[HttpGet]
public IActionResult Get()
{
try
{
// Step 1: Point to a database
uow.UseDatabase("<DB_NAME>");
//Step 2: Get a repository for your model 'Dogs'
var dogRepo = uow.GetRepository<Dogs>();
//Step 3: Do some fetch
allDogs = dogRepo.GetAll().ToList();
//Step 4: Or any CRUD operations you like
uow.BeginTransaction();
dogRepo.Add(new Dog());
uow.Commit();
return Ok(allDogs);
}
catch(Exception)
{
uow.Rollback();
}
}
}