Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/laget-se/laget.db.mongo

A repository pattern implementation for MongoDB, a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.
https://github.com/laget-se/laget.db.mongo

mongodb nuget

Last synced: 22 days ago
JSON representation

A repository pattern implementation for MongoDB, a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.

Awesome Lists containing this project

README

        

# laget.Db.Mongo
A repository pattern implementation for MongoDB, a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas.

![Nuget](https://img.shields.io/nuget/v/laget.Db.Mongo)
![Nuget](https://img.shields.io/nuget/dt/laget.Db.Mongo)

## Configuration
> This example is shown using Autofac since this is the go-to IoC for us.
```c#
public class DatabaseModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new MongoDefaultProvider(c.Resolve().GetConnectionString("MongoConnectionString"))).As().SingleInstance();
}
}
```
```c#
public class DatabaseModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new MongoDefaultProvider(c.Resolve().GetConnectionString("MongoConnectionString"), new MongoDatabaseSettings
{
ReadConcern = ReadConcern.Default,
ReadPreference = ReadPreference.SecondaryPreferred,
WriteConcern = WriteConcern.W3
})).As().SingleInstance();
}
}
```
```c#
public class DatabaseModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new MongoDefaultProvider(c.Resolve().GetConnectionString("MongoConnectionString"), new MemoryCacheOptions
{
CompactionPercentage = 0.25,
ExpirationScanFrequency = TimeSpan.FromMinutes(5),
SizeLimit = 1024
})).As().SingleInstance();
}
}
```
```c#
public class DatabaseModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new MongoDefaultProvider(c.Resolve().GetConnectionString("MongoConnectionString"), new MongoDatabaseSettings
{
ReadConcern = ReadConcern.Default,
ReadPreference = ReadPreference.SecondaryPreferred,
WriteConcern = WriteConcern.W3
}, new MemoryCacheOptions
{
CompactionPercentage = 0.25,
ExpirationScanFrequency = TimeSpan.FromMinutes(5),
SizeLimit = 1024
})).As().SingleInstance();
}
}
```

## Usage
#### Built-in methods
```c#
public interface IRepository where TEntity : Entity
{
IEnumerable Find(FilterDefinition filter);
Task> FindAsync(FilterDefinition filter);

TEntity Get(string id);
Task GetAsync(string id);
TEntity Get(FilterDefinition filter);
Task GetAsync(FilterDefinition filter);

void Insert(IEnumerable entities);
Task InsertAsync(IEnumerable entities);
void Insert(TEntity entity);
Task InsertAsync(TEntity entity);

void Update(FilterDefinition filter, UpdateDefinition update, UpdateOptions options);
Task UpdateAsync(FilterDefinition filter, UpdateDefinition update, UpdateOptions options);

void Upsert(IEnumerable entities);
Task UpsertAsync(IEnumerable entities);
void Upsert(TEntity entity);
Task UpsertAsync(TEntity entity);
void Upsert(FilterDefinition filter, TEntity entity);
Task UpsertAsync(FilterDefinition filter, TEntity entity);

void Delete(IEnumerable entities);
Task DeleteAsync(IEnumerable entities);
void Delete(TEntity entity);
Task DeleteAsync(TEntity entity);
void Delete(FilterDefinition filter);
Task DeleteAsync(FilterDefinition filter);
}
```

### Generic
```c#
public interface IUserRepository : IRepository
{
}

public class UserRepository : Repository, IUserRepository
{
public UserRepository(string connectionString)
: base(connectionString)
{
}
}
```

### Caching
```c#
public interface IUserRepository : IRepository
{
}

public class UserRepository : Repository, IUserRepository
{
public UserRepository(string connectionString)
: base(connectionString)
{
}

public override Models.User Get(string id)
{
var cacheKey = "_Id_" + id;
var item = CacheGet(cacheKey);

if (item != null)
return item;

var result = Get(id);

CacheAdd(cacheKey, result);

return result;
}
}
```