MongoDB and Redis storage.
Adds the set of conventions and ease of use for MongoDB integration with .NET Core.
dotnet add package Convey.Persistence.MongoDB
Extend IConveyBuilder
with AddMongo()
that will register the required services.
public static IConveyBuilder RegisterConvey(this IConveyBuilder builder)
{
builder.AddMongo();
// Other services.
return builder;
}
public class SomeService
{
private readonly IMongoDatabase _database;
public SomeService(IMongoDatabase database)
{
_database = database;
}
}
In order to use IMongoRepository
abstraction, invoke AddMongoRepository<TDocument, TIdentifiable>("collectionName")
for each document that you would like to be able to access with this repository abstraction and ensure that document type implements IIdentifiable
interface.
By using the provided IMongoRepository
you can access helper methods such as AddAsync()
, BrowseAsync()
etc. instead of relying on IMongoDatabase
abstraction available via MongoDB.Driver.
public class SomeService
{
private readonly IMongoRepository<SomeDocument,Guid> _repository;
public SomeService(IMongoRepository<SomeDocument,Guid> repository)
{
_repository = repository;
}
}
connectionString
- connection string e.g. mongodb://localhost:27017
.database
- database name.seed
- boolean value, if true
then IMongoDbSeeder.SeedAsync()
will be invoked (if implemented)."mongo": {
"connectionString": "mongodb://localhost:27017",
"database": "some-service",
"seed": false
}
Adds the Redis integration with .NET Core based on IDistributedCache abstraction.
dotnet add package Convey.Persistence.Redis
Extend IConveyBuilder
with AddRedis()
that will register the required services.
public static IConveyBuilder RegisterConvey(this IConveyBuilder builder)
{
builder.AddRedis();
// Other services.
return builder;
}
In order to use Redis integration, inject built-in IDistributedCache
interface.
public class SomeService
{
private readonly IDistributedCache _cache;
public SomeService(IDistributedCache cache)
{
_cache = cache;
}
}
connectionString
- connection string e.g. localhost
.instance
- optional prefix, that will be added by default to all the keys."redis": {
"connectionString": "localhost",
"instance": "some-service:"
}