Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/laget-se/laget.db.mongo
- Owner: laget-se
- License: apache-2.0
- Created: 2020-12-08T11:37:51.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-04T10:32:00.000Z (about 2 months ago)
- Last Synced: 2024-11-04T11:29:15.891Z (about 2 months ago)
- Topics: mongodb, nuget
- Language: C#
- Homepage:
- Size: 123 KB
- Stars: 0
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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;
}
}
```