Getting Started with EricksonLopez.SharedKernel
dotnet add package EricksonLopez.SharedKernel --version 4.0.0<PackageReference Include="EricksonLopez.SharedKernel" Version="4.0.0" /><PackageVersion Include="EricksonLopez.SharedKernel" Version="4.0.0" />Quick Start: Aggregate Root & Domain Events
Section titled “Quick Start: Aggregate Root & Domain Events”Define your strongly-typed ID using [StrongId<Guid>] (EricksonLopez.DomainPrimitives) or a custom IEntityId<TSelf> record struct (StrongIdGenerator was removed in v4.0.0):
using System;using EricksonLopez.Events.Contracts;using EricksonLopez.SharedKernel;
namespace Example.SharedKernelQuickStart;
public readonly record struct AccountId(Guid Value) : IEntityId<AccountId>{ public bool IsEmpty => Value == Guid.Empty; public static AccountId New() => new(Guid.NewGuid()); public static AccountId Create(Guid value) => new(value); public static AccountId Parse(string s, IFormatProvider? provider) => new(Guid.Parse(s, provider)); public static bool TryParse(string? s, IFormatProvider? provider, out AccountId result) { if (Guid.TryParse(s, provider, out var guid)) { result = new(guid); return true; }
result = default; return false; }}
public sealed record AccountOpenedEvent(AccountId AccountId) : DomainEvent;
public sealed class BankAccount : AggregateRoot<AccountId>{ public decimal Balance { get; private set; }
private BankAccount(AccountId id, decimal initialDeposit) : base(id) { Balance = initialDeposit; RaiseDomainEvent(new AccountOpenedEvent(id)); }
public static BankAccount Open(AccountId id, decimal initialDeposit) => new(id, initialDeposit);}