Table of Contents

Class EfCoreTracedAggregateEntity<TEntity, TKey>

Namespace
Savvyio.Extensions.EFCore.Domain.EventSourcing
Assembly
Savvyio.Extensions.EFCore.Domain.EventSourcing.dll

Provides a generic way for EF Core to surrogate and support an implementation of ITracedAggregateRoot<TKey>.

public class EfCoreTracedAggregateEntity<TEntity, TKey> : ITracedAggregateRoot<TKey>, IAggregateRoot<ITracedDomainEvent>, IAggregateRoot, IMetadata, IEntity<TKey>, IIdentity<TKey> where TEntity : class, IEntity<TKey>, ITracedAggregateRoot<TKey>

Type Parameters

TEntity

The type of the entity that implements the ITracedAggregateRoot<TKey> interface.

TKey

The type of the key that uniquely identifies the entity.

Inheritance
EfCoreTracedAggregateEntity<TEntity, TKey>
Implements
IEntity<TKey>
IIdentity<TKey>
Extension Methods

Examples

EfCoreTracedAggregateEntity<TEntity, TKey> is the EF Core entity class that stores one traced domain event row in the event-store table. Each row carries the aggregate ID, version, event type, and serialized event payload. The example creates a traced entity directly from an aggregate and a domain event, then reads back the stored aggregate ID and version.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using Savvyio;
using Savvyio.Domain.EventSourcing;
using Savvyio.Extensions.EFCore.Domain.EventSourcing;
using Savvyio.Handlers;

namespace ExampleApp;

public sealed class EventRecordExample
{
    public EfCoreTracedAggregateEntity<OrderTimeline, Guid> CreateRecord()
    {
        var aggregate = new OrderTimeline(Guid.NewGuid(), "PO-6001");
        var domainEvent = aggregate.Events.Single();

        return new EfCoreTracedAggregateEntity<OrderTimeline, Guid>(aggregate, domainEvent, new SimpleMarshaller());
    }
}

public sealed class OrderTimeline : TracedAggregateRoot<Guid>
{
    public OrderTimeline(Guid id, string orderNumber) : base()
    {
        AddEvent(new OrderPlaced(id, orderNumber));
    }

    private OrderTimeline(Guid id, IEnumerable<ITracedDomainEvent> events) : base(id, events)
    {
    }

    public string OrderNumber { get; private set; } = string.Empty;

    protected override void RegisterDelegates(IFireForgetRegistry<ITracedDomainEvent> handler)
    {
        handler.Register<OrderPlaced>(e =>
        {
            Id = e.OrderId;
            OrderNumber = e.OrderNumber;
        });
    }
}

public sealed record OrderPlaced(Guid OrderId, string OrderNumber) : TracedDomainEvent;

public sealed class SimpleMarshaller : IMarshaller
{
    public Stream Serialize<TValue>(TValue value)
    {
        return new MemoryStream(JsonSerializer.SerializeToUtf8Bytes(value));
    }

    public Stream Serialize(object value, Type inputType)
    {
        return new MemoryStream(JsonSerializer.SerializeToUtf8Bytes(value, inputType));
    }

    public TValue Deserialize<TValue>(Stream data)
    {
        return JsonSerializer.Deserialize<TValue>(data)!;
    }

    public object Deserialize(Stream data, Type returnType)
    {
        return JsonSerializer.Deserialize(data, returnType)!;
    }
}

Constructors

EfCoreTracedAggregateEntity(TEntity, ITracedDomainEvent, IMarshaller)

Initializes a new instance of the EfCoreTracedAggregateEntity<TEntity, TKey> class.

public EfCoreTracedAggregateEntity(TEntity aggregate, ITracedDomainEvent domainEvent, IMarshaller marshaller)

Parameters

aggregate TEntity

The traced aggregate to convert into this EF Core compatible instance.

domainEvent ITracedDomainEvent

The traced domain event to convert into this EF Core compatible instance.

marshaller IMarshaller

The IMarshaller that is used when converting ITracedDomainEvent into a serialized format.

Properties

Id

Gets the value of the identifier associated with an aggregate.

public TKey Id { get; }

Property Value

TKey

The value of the identifier associated with an aggregate.

Payload

Gets the payload of the traced domain event.

public byte[] Payload { get; }

Property Value

byte[]

The payload of the traced domain event.

Timestamp

Gets the timestamp of the traced domain event.

public DateTime Timestamp { get; }

Property Value

DateTime

The timestamp of the traced domain event.

Type

Gets the CLR type of the traced domain event.

public string Type { get; }

Property Value

string

The CLR type of the traced domain event.

Version

Gets the version of the aggregate from the traced domain event.

public long Version { get; }

Property Value

long

The version of the aggregate from the traced domain event.

See Also