MongoDB constraint (AggregateId + AggregateSequenceNumber) not created
I'm using EventFlow with MongoDB, and I noticed that the constraint (AggregateId + AggregateSequenceNumber) is not being created.
After checking the source code, I found that other database implementations like MySQL and PostgreSQL create this constraint directly via the initialization scripts.
However, for MongoDB, there is a method called Initialize() that appears to be responsible for setting up this constraint, but it doesn't seem to be called anywhere.
Is this intentional, or is it possibly a bug?
Suggestions for a fix:
In EventFlowOptionsMongoDbEventStoreExtensions.cs:
public static IEventFlowOptions UseMongoDbEventStore(this IEventFlowOptions eventFlowOptions)
{
eventFlowOptions.UseEventPersistence<MongoDbEventPersistence>();
eventFlowOptions.ServiceCollection
.TryAddTransient<IMongoDbEventPersistenceInitializer, MongoDbEventPersistenceInitializer>();
// Add this block of code
using var serviceProvider = eventFlowOptions.ServiceCollection.BuildServiceProvider();
var initializer = serviceProvider.GetRequiredService<IMongoDbEventPersistenceInitializer>();
initializer.Initialize();
return eventFlowOptions;
}
By doing this, you're creating a temporary ServiceProvider that is independent from your application's main Dependency Injection (DI) container.
This can lead to broken scopes (for example, if you have Scoped services), and might cause unexpected behavior or memory leaks.
Additionally, important dependencies like ILogger<T> might not be resolved correctly, because you're bypassing the application's actual DI lifecycle.
🛠️ These kinds of initializations should be done in the Program.cs file (or wherever you're configuring your application's service registrations) — not inside an extension method via a manually built service provider.
If you want, I can also help rewrite it properly in the Program.cs style
@AlexGreatDev Thanks for the explanation and for creating a PR! Your solution looks great to me.
Maybe it would be helpful to add a reference to how to use the MongoDbInitializer in the documentation (like here). What do you think? I’d be happy to help with that if needed.
Will get it review and merged
Hello there!
We hope you are doing well. We noticed that this issue has not seen any activity in the past 90 days. We consider this issue to be stale and will be closing it within the next seven days.
If you still require assistance with this issue, please feel free to reopen it or create a new issue.
Thank you for your understanding and cooperation.
Best regards, EventFlow
Hello there!
This issue has been closed due to inactivity for seven days. If you believe this issue still needs attention, please feel free to open a new issue or comment on this one to request its reopening.
Thank you for your contribution to this repository.
Best regards, EventFlow