Skip to content

Commit

Permalink
Add support for typed IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis committed Nov 10, 2023
1 parent c86329b commit 632de43
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Ardalis.SharedKernel/Ardalis.SharedKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<RepositoryUrl>https://github.com/ardalis/Ardalis.SharedKernel</RepositoryUrl>
<PackageTags>DDD;Shared Kernel;SharedKernel;Domain-Driven Design;Repository;Specification;ValueObject;Value Object;Ardalis;Clean;Clean Architecture;Clean Architecture Template</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<Version>1.2.0</Version>
<Version>1.3.0</Version>
<PackageReleaseNotes>
* Add Commands, Queries, and Handler interfaces
* Add EntityBase of TId
</PackageReleaseNotes>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
Expand Down
17 changes: 7 additions & 10 deletions src/Ardalis.SharedKernel/EntityBase.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace Ardalis.SharedKernel;
namespace Ardalis.SharedKernel;

/// <summary>
/// A base class for DDD Entities. Includes support for domain events dispatched post-persistence.
/// If you prefer GUID Ids, change it here.
/// If you need to support both GUID and int IDs, change to EntityBase&lt;TId&gt; and use TId as the type for Id.
/// </summary>
public abstract class EntityBase
public abstract class EntityBase : HasDomainEventsBase
{
public int Id { get; set; }
}

private List<DomainEventBase> _domainEvents = new();
[NotMapped]
public IEnumerable<DomainEventBase> DomainEvents => _domainEvents.AsReadOnly();

protected void RegisterDomainEvent(DomainEventBase domainEvent) => _domainEvents.Add(domainEvent);
internal void ClearDomainEvents() => _domainEvents.Clear();
public abstract class EntityBase<TId> : HasDomainEventsBase
where TId : struct, IEquatable<TId>
{
public TId Id { get; set; }
}

15 changes: 15 additions & 0 deletions src/Ardalis.SharedKernel/HasDomainEventsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace Ardalis.SharedKernel;

public abstract class HasDomainEventsBase
{
private List<DomainEventBase> _domainEvents = new();
[NotMapped]
public IEnumerable<DomainEventBase> DomainEvents => _domainEvents.AsReadOnly();

protected void RegisterDomainEvent(DomainEventBase domainEvent) => _domainEvents.Add(domainEvent);
internal void ClearDomainEvents() => _domainEvents.Clear();

}

0 comments on commit 632de43

Please sign in to comment.