EasyEvs - Series 1

Sat, Sep 4, 2021 2-minute read

EasyEvs in a few minutes

Setup

First we need to get EventStore running locally.

The easiest way is with docker:

docker run -d --name easy-evs-test -it -p 2113:2113 -p 1113:1113 eventstore/eventstore:21.2.0-buster-slim --insecure --run-projections=All --enable-atom-pub-over-http

Installation

Then install the EasyEvs nuget package

Install-Package EasyEvs

Configuration

Add a configuration section in your .net executable that look like this if you are using .env files for .NET2:

EasyEvs__ConnectionString=esdb://localhost:2113?tls=false

If you are not, then in json format:

"EasyEvs" :
  {
    "ConnectionString" : "esdb://localhost:2113?tls=false"
  }

The next step is to configure the library into Microsoft’s Dependency Injection ServiceCollection

services.AddEasyEvs();

That’s all we need to have injected our IEventStore to publish messages.

Now, let’s define some events shall we?

public class OrderCreated : IEvent
{
    [JsonConstructor]
    public OrderCreated(Guid id, DateTime timestamp, Guid orderId)
    {
        Id = id;
        Timestamp = timestamp;
        OrderId = orderId;
    }

    
    public Guid Id { get; }
    
    public Guid OrderId { get; }
    
    public DateTime Timestamp { get; }
    
    public string Version => "v1"

    public IReadOnlyDictionary<string, string> Metadata { get; set; }
}

Publishing events

var @event = new OrderCreated(Guid.NewGuid(), DateTime.UtcNow, orderId); 
await _eventStore.Append(@event, $"order-{@event.OrderId}", cancellationToken: CancellationToken.None);

That’s it, if you open now your browser you can find the event in the stream.

event in the stream

Let’s take a look at the event:

event details

We can see EasyEvs adds some metadata, it will need it for later, to deserialize the event for you, because that’s what we are looking, the framework, to make things easy for us.

Next we will see How to consume events