-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2182f76
commit e39eb9a
Showing
1 changed file
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,175 @@ | ||
# Welcome to Twileloop.UOW | ||
This is your GitHub Pages site. | ||
<!-- PROJECT LOGO --> | ||
<br /> | ||
<div align="center"> | ||
<a href="https://github.com/sangeethnandakumar/Twileloop.UOW"> | ||
<img src="https://iili.io/HPIj6ss.png" alt="Logo" width="80" height="80"> | ||
</a> | ||
|
||
<h1 align="center"> Twileloop.UOW</h1> | ||
<h4 align="center"> LiteDB | MongoDB </h4> | ||
</div> | ||
|
||
## 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"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/sangeethnanda) | ||
|
||
## 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 | ||
|
||
<hr/> | ||
|
||
|
||
## 2. Install Package | ||
|
||
> Choose the installation that suites your need | ||
| Driver | To Use | Install Package | ||
| :---: | :---: | :---: | ||
| <img src="https://iili.io/HPIj6ss.png" alt="Logo" height="30"> | LiteDB | `dotnet add package Twileloop.UOW.LiteDB` | ||
| <img src="https://iili.io/HPIj6ss.png" alt="Logo" height="30"> | 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) | ||
```csharp | ||
(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) | ||
```csharp | ||
//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 | ||
```csharp | ||
public class Dogs : EntityBase | ||
{ | ||
public Guid NickName { get; set; } | ||
public string Name { get; set; } | ||
} | ||
``` | ||
|
||
## 4. Inject and Use as required | ||
|
||
```csharp | ||
[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(); | ||
} | ||
} | ||
|
||
} | ||
``` |