-
-
Notifications
You must be signed in to change notification settings - Fork 10
Repositories for No SQL databases (LiteDB)
Modern generic repositories for No SQL databases are built on top one of the following popular NoSQL databases: MongoDB and LiteDB.
To use LiteDB repository install the Modern.Repositories.LiteDB.DependencyInjection
Nuget package and register it within Modern builder in DI:
builder.Services
.AddModern()
.AddRepositoriesLiteDb(options =>
{
options.AddRepository<AirplaneDbo, long>("connection_string", "collection_name");
});
Specify the type of Dbo entity model and "_id" key.
ℹ️ Right out of the box LiteDB official library supports only synchronous methods. To use asynchronous methods a third party library can be used. Modern libraries support asynchronous methods in LiteDB using litedb-async library
⚠️ DISCLAIMER: LiteDb async repository uses litedb-async library which is not an official LiteDb project. Modern libraries are NOT responsible for any problems with litedb-async library, so use this package at your own risk.
To use LiteDB Async repository install the Modern.Repositories.LiteDB.Async.DependencyInjection
Nuget package and register it within Modern builder in DI:
builder.Services
.AddModern()
.AddRepositoriesLiteDbAsync(options =>
{
options.AddRepository<AirplaneDbo, long>("connection_string", "collection_name");
});
Specify the type of Dbo entity model and "_id" key.
Modern generic repositories are flexible, easily changeable and extendable. New methods can be added to repository as well as each method can be overriden.
The following interfaces can be inherited from:
IModernCrudRepository<TEntity, TId>
IModernQueryRepository<TEntity, TId>
Or inherit from a combined interface:
IModernRepository<TEntity, TId> : IModernCrudRepository<TEntity, TId>, IModernQueryRepository<TEntity, TId>
Lets have a look at example of inheritance from ModernLiteDbRepository:
public interface IAirplaneLiteDbRepository : IModernRepository<AirplaneDbo, long>
{
Task<IEnumerable<AirplaneDbo>> GetNewAirplanesAsync();
}
public class AirplaneLiteDbRepository : ModernLiteDbRepository<AirplaneDbo, long>, IAirplaneLiteDbRepository
{
public AirplaneLiteDbRepository()
: base("lite.db", "airplanes")
{
}
protected override string GetEntityId(AirplaneDbo entity) => entity.Id;
public async Task<IEnumerable<AirplaneDbo>> GetNewAirplanesAsync()
{
await Task.CompletedTask;
using var db = new LiteDatabase("lite.db");
var collection = db.GetCollection<AirplaneMongo>("airplanes");
return collection.Find(x => x.YearOfManufacture > 2010).ToList();
}
}
Lets have a look at example of inheritance from ModernLiteDbAsyncRepository:
public interface IAirplaneLiteDbRepository : IModernRepository<AirplaneDbo, long>
{
Task<IEnumerable<AirplaneDbo>> GetNewAirplanesAsync();
}
public class AirplaneLiteDbRepository : ModernLiteDbAsyncRepository<AirplaneDbo, long>, IAirplaneLiteDbRepository
{
public AirplaneLiteDbRepository()
: base("lite.db", "airplanes")
{
}
protected override string GetEntityId(AirplaneDbo entity) => entity.Id;
public async Task<IEnumerable<AirplaneDbo>> GetNewAirplanesAsync()
{
using var db = new LiteDatabase("lite.db");
var collection = db.GetCollection<AirplaneMongo>("airplanes");
return await collection.FindAsync(x => x.YearOfManufacture > 2010);
}
}