EventFlow.MongoDB offers MongoDB functionality to the EventFlow package.
- ReadStores - create readmodels which are persisted in MongoDB
- SnapshotStore - create snapshots which are persisted in MongoDB
- EventStore - events are persited in MongoDB
run the following command in the Package Manager Console:
Install-Package EventFlow.MongoDB
Use the ConfigureMongoDb(mongoUrl, mongodb) extension method on the IEventFlowOptions.
var resolver = EventFlowOptions.New
...
.ConfigureMongoDb(mongoUrl, mongodb)
...
.CreateResolver(true);Where mongoUrl is valid MongoDB connection string and mongodb the name of the database.
[MongoDbCollectionName("users")]
public class UserReadModel : IMongoDbReadModel,
IAmReadModelFor<UserAggregate, UserId, UserCreated>
{
public string _id { get; set; }
public long? _version { get; set; }
public string Username { get; set; }
public void Apply(
IReadModelContext context,
IDomainEvent<UserAggregate, UserId, UserCreated> domainEvent)
{
_id = domainEvent.AggregateIdentity.Value;
Username = domainEvent.AggregateEvent.Username.Value;
}
}public interface IUserReadModelLocator : IReadModelLocator { }
public class UserReadModelLocator : IUserReadModelLocator
{
public IEnumerable<string> GetReadModelIds(IDomainEvent domainEvent)
{
yield return "some id based on some event";
}
}Without ReadModelLocator:
var resolver = EventFlowOptions.New
...
.UseMongoDbReadModel<UserReadModel>()
...
.CreateResolver(true);With ReadModelLocator:
var resolver = EventFlowOptions.New
...
.RegisterServices(sr =>
{
sr.Register<IUserReadModelLocator, UserReadModelLocator>();
})
.UseMongoDbReadModel<UserReadModel, IUserReadModelLocator>()
...
.CreateResolver(true);var resolver = EventFlowOptions.New
...
.UseMongoDbSnapshotStore()
...
.CreateResolver(true);There's currently 2 types of read models:
- IMongoDbReadModel
- IMongoDbInserOnlyReadModel
Configuring EventFlow is identical for both, you only have to implement the proper interface to get the required functionality.
Works as a normal IReadModel and it will update the document corresponding with the _id property.
Every event that is applied will result in a Mongo document being inserted in the collection. A use case for this would be an account ledger where you want to store every single transaction that happens.
var resolver = EventFlowOptions.New
...
.UseMongoDbEventStore()
...
.CreateResolver(true);