PiBox.Plugins.Persistence.MongoDb is a plugin
that allows other PiBox components
to use the MongoDb core to interact with certain databases
.
To install the nuget package follow these steps:
dotnet add package PiBox.Plugins.Persistence.MongoDb
or add as package reference to your .csproj
<PackageReference Include="PiBox.Plugins.Persistence.MongoDb" Version="" />
Configure your appsettings.yml with these properties
Postgres for example
mongodb:
host: "localhost"
database: "mongodb"
port: "27017"
password: "mongodb"
user: "mongodb"
MongoDbConfiguration.cs
[Configuration("mongodb")]
public class MongoDbConfiguration
{
public string Host { get; set; } = null!;
public int Port { get; set; }
public string Database { get; set; } = null!;
public string AuthDatabase { get; set; } = null!;
public string User { get; set; } = null!;
public string Password { get; set; } = null!;
}
Nothing to configure
public class ExampleService
{
private readonly IReadRepository<MyPoco> _readRepository;
public ExampleService(IReadRepository<MyPoco> readRepository)
{
_readRepository = readRepository;
}
public async Task DoStuff()
{
// for example
var result = await _readRepository.FindByIdAsync();
}
}
public class ExampleService
{
private readonly IMongoDbInstance _mongoDbInstance;
public ExampleService(IMongoDbInstance mongoDbInstance)
{
_mongoDbInstance = mongoDbInstance;
}
public async Task DoStuff()
{
// for example
var mongoCollection = _mongoDbInstance.GetCollectionFor<MyCollectionEntity>();
}
}