Skip to content

Commit

Permalink
Give direct access to supported EventTypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Apr 25, 2024
1 parent bba27f3 commit 3dd1d43
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion props/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PackageTags>qowaiv domain model</PackageTags>
<Company>Qowaiv community</Company>
<Copyright>Copyright © Qowaiv community 2013-current</Copyright>
<LangVersion>11.0</LangVersion>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<!-- We ship package versions for obsolete target frameworks too -->
<CheckEolTargetFramework>false</CheckEolTargetFramework>
Expand Down
22 changes: 21 additions & 1 deletion src/Qowaiv.DomainModel/Aggregate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Qowaiv.DomainModel;
using System.Reflection;

namespace Qowaiv.DomainModel;

/// <summary>Factory method for creating <see cref="Aggregate{TAggregate, TId}"/> from stored events.</summary>
public static class Aggregate
Expand All @@ -23,4 +25,22 @@ public static TAggregate FromStorage<TAggregate, TId>(EventBuffer<TId> buffer)
aggregate.Replay(buffer);
return aggregate;
}

/// <inheritdoc cref="EventDispatcher.SupportedEventTypes" />
[Pure]
public static ReadOnlySet<Type> SupportedEventTypes<TAggregate>()
where TAggregate : Aggregate<TAggregate>, new()
{
return Dispatcher(new TAggregate()).SupportedEventTypes;

EventDispatcher Dispatcher(TAggregate aggregate)
=> (EventDispatcher)typeof(TAggregate)
.GetProperty(nameof(Dispatcher), NonPublic)!
.GetValue(aggregate)!;
}

#pragma warning disable S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
// We own the code.
private const BindingFlags NonPublic = BindingFlags.Instance | BindingFlags.NonPublic;
#pragma warning restore S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
}
4 changes: 3 additions & 1 deletion src/Qowaiv.DomainModel/Qowaiv.DomainModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0;net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<PackageReleaseNotes>
v1.0.2
- Expose supported event types via Aggregate.SupportedEventTypes().
v1.0.1
- Reintroduce If() on Then(). #40
v1.0.0
Expand Down
19 changes: 19 additions & 0 deletions test/Qowaiv.DomainModel.UnitTests/Supported_EventTypes_specs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Qowaiv.DomainModel;

namespace Supported_EventTypes_specs;

public class Exposes
{
[Test]
public void Supported_EventTypes_of_protected_dispatcher()
{
Aggregate.SupportedEventTypes<SimpleEventSourcedAggregate>()
.Should().BeEquivalentTo(
[
typeof(NameUpdated),
typeof(DateOfBirthUpdated),
typeof(SimpleInitEvent),
typeof(InvalidEvent),
]);
}
}

0 comments on commit 3dd1d43

Please sign in to comment.