Added EventStoreDB E-Commerce initial samples
E-Commerce sample of Event Sourcing with EvenStoreDB
Sample is showing the typical flow of the Event Sourcing app with EventStoreDB.
Prerequisities
- Install git - https://git-scm.com/downloads.
- Install .NET Core 5.0 - https://dotnet.microsoft.com/download/dotnet/5.0.
- Install Visual Studio 2019, Rider or VSCode.
- Install docker - https://docs.docker.com/docker-for-windows/install/.
- Open
ECommerce.sln
solution.
Running
- Go to docker and run:
docker-compose up
. - Wait until all dockers got are downloaded and running.
- You should automatically get:
- Postgres DB running
- PG Admin - IDE for postgres. Available at: http://localhost:5050.
- Login:
admin@pgadmin.org
, Password:admin
- To connect to server Use host:
postgres
, user:postgres
, password:Password12!
- Login:
- EventStoreDB UI: http://localhost:2113/
- Open, build and run
ECommerce.sln
solution.- Swagger should be available at: http://localhost:5000/index.html
Overview
It uses:
- CQRS with MediatR,
- Stores events from Aggregate method results to EventStoreDB,
- Builds read models using Subscription to
$all
. - Read models are stored as Marten documents.
- App has Swagger and predefined docker-compose to run and play with samples.
Write Model
- Most of the write model infrastructure was reused from other samples,
- Added new project
Core.EventStoreDB
for specific EventStoreDB code, - Added EventStoreDBRepository repository to load and store aggregate state,
- Added separate IProjection interface to handle the same way stream aggregation and materialised projections,
- Thanks to that added dedicated AggregateStream method for stream aggregation
- See sample Aggregate
Read Model
- Read models are rebuilt with eventual consistency using subscribe to all EventStoreDB feature,
- Added hosted service SubscribeToAllBackgroundWorker to handle subscribing to all. It handles checkpointing and simple retries if the connection was dropped.
- Added ISubscriptionCheckpointRepository for handling Subscription checkpointing.
- Added checkpointing to EventStoreDB stream with EventStoreDBSubscriptionCheckpointRepository and dummy in-memory checkpointer InMemorySubscriptionCheckpointRepository,
- Added MartenExternalProjection as a sample how to project with
left-fold
into external storage. Another (e.g. ElasticSearch, EntityFramework) can be implemented the same way.
Tests
- Added sample of unit testing in
Carts.Tests
: - Added sample of integration testing in
Carts.Api.Tests
Other
- Added EventTypeMapper class to allow both convention-based mapping (by the .NET type name) and custom to handle event versioning,
- Added StreamNameMapper class for convention-based id (and optional tenant) mapping based on the stream type and module,
- IoC registration helpers for EventStoreDB configuration,
Trivia
- Docker useful commands
docker-compose up
- start dockersdocker-compose kill
- to stop running dockers.docker-compose down -v
- to clean stopped dockers.docker ps
- for showing running dockersdocker ps -a
- to show all dockers (also stopped)