View on GitHub

Twileloop.Uow

Readymade Repository for LiteDB & MongoDB

Open Sourced And Maintained By Sangeeth Nandakumar
Download this project as a .zip file Download this project as a tar.gz file

πŸ”™ 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!

"Buy Me A Coffee"

Usage

To get started, You have to select which package to install:


2. Install Package

Choose the installation that suites your need

Driver To Use Install Package
Logo LiteDB dotnet add package Twileloop.UOW.LiteDB
Logo 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();
            }            
        }

    }