Class ModelBuilderExtensions
- Namespace
- Savvyio.Extensions.EFCore.Domain.EventSourcing
- Assembly
- Savvyio.Extensions.EFCore.Domain.EventSourcing.dll
Extension methods for the ModelBuilder class.
public static class ModelBuilderExtensions
- Inheritance
-
ModelBuilderExtensions
Examples
This example shows how AddEventSourcing can be applied in OnModelCreating to register the event table for a traced aggregate.
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Savvyio.Domain.EventSourcing;
using Savvyio.Extensions.EFCore;
using Savvyio.Extensions.EFCore.Domain.EventSourcing;
using Savvyio.Handlers;
namespace ExampleApp;
public sealed class EventStoreContextFactory
{
public EventStoreContext Create()
{
return new EventStoreContext(new EfCoreDataSourceOptions
{
ContextConfigurator = builder => builder.EnableDetailedErrors()
});
}
}
public sealed class EventStoreContext : EfCoreDbContext
{
public EventStoreContext(EfCoreDataSourceOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.AddEventSourcing<OrderTimeline, Guid>(options => options.TableName = "OrderTimelineEvents");
}
}
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)
{
}
protected override void RegisterDelegates(IFireForgetRegistry<ITracedDomainEvent> handler)
{
handler.Register<OrderPlaced>(_ => { });
}
}
public sealed record OrderPlaced(Guid OrderId, string OrderNumber) : TracedDomainEvent;
Methods
AddEventSourcing<TEntity, TKey>(ModelBuilder, Action<EfCoreTracedAggregateEntityOptions>)
Adds a default implementation of an EF Core compatible Event Sourcing model.
public static ModelBuilder AddEventSourcing<TEntity, TKey>(this ModelBuilder mb, Action<EfCoreTracedAggregateEntityOptions> setup = null) where TEntity : class, ITracedAggregateRoot<TKey>
Parameters
mbModelBuilderThe ModelBuilder to extend.
setupAction<EfCoreTracedAggregateEntityOptions>The EfCoreTracedAggregateEntityOptions which may be configured.
Returns
- ModelBuilder
A reference to
mbso that additional configuration calls can be chained.
Type Parameters
TEntityThe type of the entity that implements the ITracedAggregateRoot<TKey> interface.
TKeyThe type of the key that uniquely identifies the entity.