Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Id Generation

Marc Wittke edited this page Sep 29, 2018 · 1 revision

Identity columns are a thing, but a real DDD implementation does not accept key values to be changed during the life time of an object. So, when you call a constructor, you have to provide an Id. Full stop.

To help you to reach this goal, a HiLo Id Generator implementation is available in Backend.Fx.Patterns.IdGenerations. Implementing it using a database sequence and Backend.Fx.EfCorePersistence is easy as follows:

public class AppEntityIdSequence : MsSqlSequence
{
    public override int Increment { get; } = 1000;
    protected override string SequenceName { get; } = "EntityId";
}

public class EntityIdGenerator : SequenceHiLoIdGenerator<AppDbContext>, IEntityIdGenerator
{
    public EntityIdGenerator(DbContextOptions<AppDbContext> dbContextOptions) 
            : base(new AppEntityIdSequence(), dbContextOptions)
    { }

    protected override int Increment { get; } = 1000;
}

The framework will take care of creation of the sequence.

Clone this wiki locally