Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strongly typed IDs not supported by Marten's event store? #3306

Open
aradalvand opened this issue Jul 11, 2024 · 5 comments
Open

Strongly typed IDs not supported by Marten's event store? #3306

aradalvand opened this issue Jul 11, 2024 · 5 comments

Comments

@aradalvand
Copy link

aradalvand commented Jul 11, 2024

I thought support for this was added following #2487 but I can't seem to get a pretty simple sample working.

Types.cs:

using System.Text.Json.Serialization;
using Vogen;

namespace Foo;

[ValueObject<Guid>(toPrimitiveCasting: CastOperator.Implicit)]
public readonly partial struct PaymentId;

public class Payment
{
    [JsonInclude]
    public PaymentId Id { get; private set; }
    [JsonInclude]
    public DateTimeOffset CreatedAt { get; private set; }
    [JsonInclude]
    public PaymentState State { get; private set; }

    public static Payment Create(PaymentCreated @event) => new()
    {
        Id = @event.Id,
        CreatedAt = @event.CreatedAt,
        State = PaymentState.Created,
    };

    public void Apply(PaymentCanceled @event)
    {
        State = PaymentState.Canceled;
    }

    public void Apply(PaymentVerified @event)
    {
        State = PaymentState.Verified;
    }
}

public enum PaymentState
{
    Created,
    Initialized,
    Canceled,
    Verified,
}

public record PaymentCreated(
    PaymentId Id,
    DateTimeOffset CreatedAt
);

public record PaymentCanceled(
    PaymentId Id,
    DateTimeOffset CanceledAt
);

public record PaymentVerified(
    PaymentId Id,
    DateTimeOffset VerifiedAt
);

Program.cs:

using Foo;
using Marten;
using Marten.Events.Daemon.Resiliency;
using Marten.Events.Projections;
using Weasel.Core;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMarten(options =>
{
    options.Connection(
        "Host=localhost;Database=marten_event_store_test;Username=arad;Password=1301381"
    );

    options.UseSystemTextJsonForSerialization(options: new()
    {
        IncludeFields = true,
    });

    if (builder.Environment.IsDevelopment())
        options.AutoCreateSchemaObjects = AutoCreate.All;

    options.Projections.Snapshot<Payment>(SnapshotLifecycle.Inline);

    options.Events.AddEventType<PaymentCreated>();
    options.Events.AddEventType<PaymentVerified>();
    options.Events.AddEventType<PaymentCanceled>();
})
.AddAsyncDaemon(DaemonMode.HotCold)
.UseLightweightSessions();

var app = builder.Build();

var paymentId = PaymentId.From(Guid.Parse("eb5b8626-973f-41f0-922d-4a4303eeb625"));
app.MapGet("/", async (IDocumentSession session) =>
{
    var r2 = await session.Events.FetchForWriting<Payment>(paymentId);
    return r2.Aggregate;
});
app.MapPost("/", async (IDocumentSession session) =>
{
    session.Events.Append(paymentId, new PaymentCreated(
        paymentId,
        DateTimeOffset.Now
    ));
    session.Events.Append(paymentId, new PaymentCanceled(
        paymentId,
        DateTimeOffset.Now
    ));
    await session.SaveChangesAsync();
    return "done";
});

app.Run();

Upon dotnet run, I get the following exception:

Unhandled exception. Marten.Exceptions.InvalidProjectionException: Id type mismatch. The stream identity type is System.Guid, but the aggregate document Foo.Payment id type is PaymentId
   at Marten.Events.Projections.ProjectionOptions.AssertValidity(DocumentStore store)
   at Marten.DocumentStore..ctor(StoreOptions options)
   at Marten.MartenServiceCollectionExtensions.<>c.<AddMarten>b__8_1(IServiceProvider s)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Marten.MartenServiceCollectionExtensions.MartenConfigurationExpression.<>c.<AddAsyncDaemon>b__8_1(IServiceProvider s)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at Program.<Main>$(String[] args) in /home/arad/scratchpad/marten-vogen-test/Program.cs:line 54
@matthewtamm
Copy link

matthewtamm commented Jul 11, 2024 via email

@mysticmind
Copy link
Member

There is not yet any direct support for strong typed identifiers for the event store

Screenshot 2024-07-11 at 12 48 36 PM

@aradalvand
Copy link
Author

aradalvand commented Jul 11, 2024

Ah, okay. Thanks for the pointers. I didn't see that callout.
Huge bummer though; this is a deal breaker for a lot of us and it effectively means we couldn't use Marten at all.

Is there an issue tracking this limitation or could we leave this one open to do so?

@aradalvand aradalvand changed the title Id type mismatch exception when using strongly typed IDs Strongly typed IDs not supported by Marten's event store? Jul 11, 2024
@mysticmind
Copy link
Member

mysticmind commented Jul 11, 2024

Let us just leave this issue open.

@jeremydmiller
Copy link
Member

jeremydmiller commented Jul 12, 2024

@aradalvand C'mon, this was documented.

"Huge bummer though; this is a deal breaker for a lot of us and it effectively means we couldn't use Marten at all."

That's absolutely and completely bonkers IMO. Why does this even matter, really? Are you wanting that to maybe distinguish between different aggregates?

And just so everybody understands what an insane amount of work it was to add what's already there, check out the commit history for the document db side of this:

#3268

I hate all of you. This is so much work for so very, very little value for just a handful of people who desperately love unnecessary complexity. But let's get this done so people stop complaining about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants