Event Sourcing with EventFlow and Azure Cosmos DB
In our Dev Camp this year we chose to experiment with Event Sourcing, using EventFlow for the solution. One task was to configure it to use an Azure Cosmos database instead of a SQL Server one; the initial arrangement in one of the sample programs we started with was SQL via Entity Framework.
SQL via Entity Framework
EventFlow supports Entity Framework, as may be seen from the corresponding NuGet package:
It’s configured as follows:
where EventStoreContext and EventStoreContextProvider are both set up to use SQL Server:
The connection string is something like the usual
Azure Cosmos – take 1
The simplest option seemed to be to swap out SQL in favour of Cosmos in EventStoreContext and EventStoreContextProvider using the appropriate NuGet package:
and the code
Unfortunately, this resulted in the following runtime error:
The combination of EventFlow and Entity Framework resulted in the need for the target database to have some distinctly SQL-like properties such as, in this instance, an identity column.
Azure Cosmos – take 2
This failure suggested an approach of bypassing Entity Framework and getting EventFlow to use a Cosmos database directly. Sadly …
EventFlow doesn’t support Cosmos databases (at least not yet).
Azure Cosmos – take 3
However, EventFlow can communicate with a Mongo database:
and a Cosmos database can be treated as if it was a Mongo database. Here’s the first tab of the Azure Cosmos DB Emulator:
Using that, we were able to change from
with a little help from https://libraries.io/nuget/EventFlow.MongoDB.
This was successful and finally the application could connect to the Cosmos database without error.
Event Flow Application
We used one from https://github.com/twzhangyang/RestAirline, although this was far from complete and working. It uses EventFlow’s support of Entity Framework to communicate with a SQL database.
Entity Framework and Cosmos
uses a SQL Server write database and a Cosmos database for reading. However, it
wasn’t directly applicable to our desired architecture.
Event Flow Racetimes Example
https://github.com/dennisfabri/Eventflow.Example.Racetimes was a sample application in which Event Flow was used with a SQL database either via Entity Framework or directly. It was used as a testing ground to see if we could configure an application to use a Mongo database instead of a SQL one (neither via Entity Framework). This was a helpful halfway house when we were trying to reconfigure our application to
- Not use Entity Framework and
- Use a Mongo database instead of a SQL one
The Azure Cosmos DB Emulator provides a cost-free way to develop against a Cosmos database. As noted above, you can also connect to a Cosmos database as if it was a Mongo database – the appropriate connection string is provided by the emulator. However, this facility is turned off by default – you need to start the emulator with the EnableMongoDbEndpoint command line parameter to be able to use it.
The emulator also likes to claim that the cloud is a bit busy if you ask it to do something it doesn’t want to do:
"Sorry, we are currently experiencing high demand in this region, and cannot fulfil your request at this time. We work continuously to bring more and more capacity online, and encourage you to try again shortly."
It tends to do this irrespective of the actual cause (such as adding too many databases to your emulator).