Home

Awesome

EventFlow

EventFlow logo

$ dotnet add package EventFlow

EventFlow is a basic CQRS+ES framework designed to be easy to use.

Have a look at our getting started guide, the do’s and don’ts and the FAQ.

Alternatively, join our Discord server to engage with the community. Its hopefully getting a reboot to kickstart the upcoming release of v1.

Features

Versions

Development of version 1.0 has started and is mainly braking changes regarding changes related to replacing EventFlow types with that of Microsoft extension abstractions, mainly IServiceProvider and ILogger<>.

The following list key characteristics of each version as well as its related branches (not properly configured yet).

Talks directly related to EventFlow

Examples

External Examples

List of examples create by different community members. Note that many of these examples will be using EventFlow 0.x.

Create a pull request to get your exampled linked from here.

Overview

Here is a list of the EventFlow concepts. Use the links to navigate to the documentation.

Complete example

Here's a complete example on how to use the default in-memory event store along with an in-memory read model.

The example consists of the following classes, each shown below

Note: This example is part of the EventFlow test suite, so checkout the code and give it a go.

[Test]
public async Task Example()
{
  // We wire up EventFlow with all of our classes. Instead of adding events,
  // commands, etc. explicitly, we could have used the the simpler
  // AddDefaults(Assembly) instead.
  var serviceCollection = new ServiceCollection()
    .AddLogging()
    .AddEventFlow(o => o
      .AddEvents(typeof(ExampleEvent))
      .AddCommands(typeof(ExampleCommand))
      .AddCommandHandlers(typeof(ExampleCommandHandler))
      .UseInMemoryReadStoreFor<ExampleReadModel>());

  using (var serviceProvider = serviceCollection.BuildServiceProvider())
  {
    // Create a new identity for our aggregate root
    var exampleId = ExampleId.New;

    // Resolve the command bus and use it to publish a command
    var commandBus = serviceProvider.GetRequiredService<ICommandBus>();
    await commandBus.PublishAsync(
      new ExampleCommand(exampleId, 42), CancellationToken.None);

    // Resolve the query handler and use the built-in query for fetching
    // read models by identity to get our read model representing the
    // state of our aggregate root
    var queryProcessor = serviceProvider.GetRequiredService<IQueryProcessor>();
    var exampleReadModel = await queryProcessor.ProcessAsync(
      new ReadModelByIdQuery<ExampleReadModel>(exampleId), CancellationToken.None);

    // Verify that the read model has the expected magic number
    exampleReadModel.MagicNumber.Should().Be(42);
  }
}
// The aggregate root
public class ExampleAggregate : AggregateRoot<ExampleAggregate, ExampleId>,
  IEmit<ExampleEvent>
{
  private int? _magicNumber;

  public ExampleAggregate(ExampleId id) : base(id) { }

  // Method invoked by our command
  public void SetMagicNumber(int magicNumber)
  {
    if (_magicNumber.HasValue)
      throw DomainError.With("Magic number already set");

    Emit(new ExampleEvent(magicNumber));
  }

  // We apply the event as part of the event sourcing system. EventFlow
  // provides several different methods for doing this, e.g. state objects,
  // the Apply method is merely the simplest
  public void Apply(ExampleEvent aggregateEvent)
  {
    _magicNumber = aggregateEvent.MagicNumber;
  }
}
// Represents the aggregate identity (ID)
public class ExampleId : Identity<ExampleId>
{
  public ExampleId(string value) : base(value) { }
}
// A basic event containing some information
public class ExampleEvent : AggregateEvent<ExampleAggregate, ExampleId>
{
  public ExampleEvent(int magicNumber)
  {
      MagicNumber = magicNumber;
  }

  public int MagicNumber { get; }
}
// Command for update magic number
public class ExampleCommand : Command<ExampleAggregate, ExampleId>
{
  public ExampleCommand(
    ExampleId aggregateId,
    int magicNumber)
    : base(aggregateId)
  {
    MagicNumber = magicNumber;
  }

  public int MagicNumber { get; }
}
// Command handler for our command
public class ExampleCommandHandler
  : CommandHandler<ExampleAggregate, ExampleId, ExampleCommand>
{
  public override Task ExecuteAsync(
    ExampleAggregate aggregate,
    ExampleCommand command,
    CancellationToken cancellationToken)
  {
    aggregate.SetMagicNumber(command.MagicNumber);
    return Task.CompletedTask;;
  }
}
// Read model for our aggregate
public class ExampleReadModel : IReadModel,
  IAmReadModelFor<ExampleAggregate, ExampleId, ExampleEvent>
{
  public int MagicNumber { get; private set; }

  public Task ApplyAsync(
    IReadModelContext context,
    IDomainEvent<ExampleAggregate, ExampleId, ExampleEvent> domainEvent,
    CancellationToken _cancellationToken
  {
    MagicNumber = domainEvent.AggregateEvent.MagicNumber;
    return Task.CompletedTask;
  }
}

State of EventFlow

EventFlow is still under development, especially the parts regarding how read models are re-populated.

EventFlow is currently used in production environments and performs very well, but it needs to mature before key APIs are stable.

EventFlow is greatly opinionated, but it's possible to create new implementations for almost every part of EventFlow by registering a different implementation of an interface.

Useful articles related to EventFlow and DDD

Many of the technical design decisions in EventFlow is based on articles. This section lists some of them. If you have a link with a relevant article, please share it by creating an issue with the link.

Integration tests

EventFlow has several tests that verify that its ability to use the systems it integrates with correctly.

To setup a local test environment run the following commands in the checkout directory of EventFlow.

docker-compose pull
docker-compose up

Alternatively, you can skip the NUnit tests marked with the integration category.

Thanks

<table border="0" cellpadding="0" cellspacing="0"> <tr> <td width="25%"> <a href="https://www.jetbrains.com/"><img src="./Resources/jetbrains-128x128.png" /></a> </td> </tr> </table>

License

EventFlow was originally developed <u>in my spare time</u> while I worked at both <a href="https://www.ebay.com/">eBay (2015 to 2021)</a> and <a href="https://schibsted.com/">Schibsted (2021 and onward)</a>.

The MIT License (MIT)

Copyright (c) 2015-2024 Rasmus Mikkelsen
https://github.com/eventflow/EventFlow

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.