Skip to content

Commit

Permalink
Fix for strong typed identifier usage w/ CustomProjection
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Feb 18, 2025
1 parent 769e03d commit 63eaa10
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
57 changes: 57 additions & 0 deletions src/EventSourcingTests/Projections/identity_from_event.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using EventSourcingTests.Aggregation;
using Marten.Events;
using Shouldly;
using StronglyTypedIds;
using Xunit;

namespace EventSourcingTests.Projections;

public class identity_from_event
{
[Fact]
public void by_string()
{
var e = new Event<AEvent>(new AEvent()) { StreamKey = "foo" };

e.IdentityFromEvent<string>(StreamIdentity.AsString)
.ShouldBe("foo");
}

[Fact]
public void by_guid()
{
var streamId = Guid.NewGuid();
var e = new Event<AEvent>(new AEvent()) { StreamId = streamId };

e.IdentityFromEvent<Guid>(StreamIdentity.AsGuid)
.ShouldBe(streamId);
}

[Fact]
public void by_strong_typed_guid()
{
var streamId = Guid.NewGuid();
var e = new Event<AEvent>(new AEvent()) { StreamId = streamId };

e.IdentityFromEvent<TripId>(StreamIdentity.AsGuid)
.Value.ShouldBe(streamId);
}

[Fact]
public void by_strong_typed_string()
{
var e = new Event<AEvent>(new AEvent()) { StreamKey = "bar" };

e.IdentityFromEvent<EventSourcingTests.StringId>(StreamIdentity.AsString)
.Value.ShouldBe("bar");
}


}

[StronglyTypedId(Template.Guid)]
public partial struct TripId;

[StronglyTypedId(Template.String)]
public partial struct StringId;
5 changes: 3 additions & 2 deletions src/Marten/Events/Aggregation/CustomProjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Marten.Events.Daemon.Internals;
using Marten.Events.Projections;
using Marten.Exceptions;
using Marten.Internal;
using Marten.Internal.Sessions;
using Marten.Internal.Storage;
using Marten.Schema;
Expand Down Expand Up @@ -419,6 +420,6 @@ public IAggregateCache<TId, TDoc> CacheFor(Tenant tenant)
}
}

public TId IdentityFromEvent(StreamIdentity streamIdentity, IEvent e) =>
streamIdentity == StreamIdentity.AsGuid ? e.StreamId.To<TId>() : e.StreamKey.To<TId>();
public TId IdentityFromEvent(StreamIdentity streamIdentity, IEvent e)
=> e.IdentityFromEvent<TId>(streamIdentity);
}
25 changes: 25 additions & 0 deletions src/Marten/Events/Event.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using JasperFx.Core.Reflection;
using Marten.Storage;

namespace Marten.Events;
Expand Down Expand Up @@ -250,4 +251,28 @@ public static IEvent<T> WithData<T>(this IEvent @event, T eventData) where T : n
Timestamp = @event.Timestamp
};
}

public static TId IdentityFromEvent<TId>(this IEvent e, StreamIdentity streamIdentity)
{
if (streamIdentity == StreamIdentity.AsGuid)
{
if (typeof(TId) == typeof(Guid))
{
return e.StreamId.As<TId>();
}

var valueTypeInfo = new StoreOptions().RegisterValueType(typeof(TId));
return valueTypeInfo.CreateAggregateIdentitySource<TId>()(e);
}
else
{
if (typeof(TId) == typeof(string))
{
return e.StreamKey.As<TId>();
}

var valueTypeInfo = new StoreOptions().RegisterValueType(typeof(TId));
return valueTypeInfo.CreateAggregateIdentitySource<TId>()(e);
}
}
}

0 comments on commit 63eaa10

Please sign in to comment.