Repository pattern for the official MongoDB .NET Core driver.
The package is available on NuGet.
dotnet add package MongoDB.Extensions.Repository
For basic, generic access, inject IMongoRepository<TEntity>
into your services for example:
public class SomeService
{
private readonly IMongoRepository<SomeEntity> _repository;
public SomeService(IMongoRepository<SomeEntity> repository)
{
_repository = repository;
}
public async Task<SomeEntity> GetSomeDataAsync(string id) => await _repository.GetAsync(id);
}
By default, collections will be named in lower snake_case
.
Configuration is easy in ASP.Net Core applications. In Startup.cs
:
public void ConfigureServices(IServiceCollection services)
{
var assembly = typeof(Startup).Assembly;
services.AddMongoRepositories(_configuration.GetConnectionString("mongo"))
.FromAssembly(assembly) // Registers all classes that implement IMongoRepository<TEntity>
.WithIndexesFromAssembly(assembly); // Registers all classes that implement MongoIndexProfile<TEntity>
}
Configured indexes will be created the first time that a collection is accessed.
To configure indexes, implement MongoIndexProfile<TEntity>
for example, to create an ascending, case insensitive, unique index on SomeEntity.Name
called name_unique
:
public class SomeIndexProfile : MongoIndexProfile<SomeEntity>
{
public SomeIndexProfile()
{
Add("name_unique", IndexKeys.Ascending(x => x.Name), o => o.Unique().WithCaseInsensitiveCollation());
}
}