Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/Persistence/MartenTests/strong_typed_identifiers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using IntegrationTests;
using Marten;
using Microsoft.Extensions.Hosting;
using Shouldly;
using StronglyTypedIds;
using Wolverine;
using Wolverine.Marten;
using Wolverine.Persistence;

namespace MartenTests;

public class strong_typed_identifiers : PostgresqlContext, IAsyncLifetime
{
private IHost _host;

public async Task InitializeAsync()
{
_host = await Host.CreateDefaultBuilder()
.UseWolverine(opts =>
{
opts.Services.AddMarten(m =>
{
m.Connection(Servers.PostgresConnectionString);
m.DatabaseSchemaName = "knobs";
}).IntegrateWithWolverine();
}).StartAsync();
}

public async Task DisposeAsync()
{
await _host.StopAsync();
}

[Fact]
public async Task use_strong_typed_identifier_with_single_entity_attribute()
{
var knob1 = new Knob() { Name = "Single" };
using var session = _host.DocumentStore().LightweightSession();
session.Store(knob1);
await session.SaveChangesAsync();

await _host.InvokeAsync(new TwistKnob(knob1.Id));
}

[Fact]
public async Task use_with_multiple_entities_so_it_has_to_use_batch_querying()
{
var knob1 = new Knob() { Name = "One" };
var knob2 = new Knob() { Name = "Two" };
using var session = _host.DocumentStore().LightweightSession();
session.Store(knob1, knob2);
await session.SaveChangesAsync();

await _host.InvokeAsync(new TwistOneThenAnother(knob1.Id, knob2.Id));
}
}

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

public class Knob
{
public KnobId Id { get; set; }
public string Name { get; set; }
}

public record TwistKnob(KnobId Id);
public record TwistOneThenAnother(KnobId Id1, KnobId Id2);

public static class KnobHandler
{
public static void Handle(TwistKnob command, [Entity] Knob knob)
{
knob.ShouldNotBeNull();
knob.Name.ShouldBe("Single");
}

public static void Handle(
TwistOneThenAnother command,
[Entity("Id1")] Knob knob1,
[Entity("Id2")] Knob knob2

)
{
knob1.Name.ShouldBe("One");
knob2.Name.ShouldBe("Two");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Wolverine.Marten.Persistence.Sagas;
internal class LoadDocumentFrame : AsyncFrame, IBatchableFrame
{
public const string ExpectedSagaRevision = "expectedSagaRevision";
private static Type[] _identityTypes = [typeof(int), typeof(long), typeof(string), typeof(Guid)];

private readonly Variable _sagaId;
private Variable? _cancellation;
Expand All @@ -23,16 +24,34 @@ public LoadDocumentFrame(Type sagaType, Variable sagaId)
_sagaId = sagaId;
uses.Add(sagaId);

Saga = new Variable(sagaType, this);
var usage = $"{Variable.DefaultArgName(sagaType)}_{sagaId.Usage.Split('.').Last()}";
Saga = new Variable(sagaType, usage, this);
}

public void WriteCodeToEnlistInBatchQuery(GeneratedMethod method, ISourceWriter writer)
{
if (_batchQueryItem == null)
throw new InvalidOperationException("This frame has not been enlisted in a MartenBatchFrame");

writer.Write(
$"var {_batchQueryItem.Usage} = {_batchQuery!.Usage}.Load<{Saga.VariableType.FullNameInCode()}>({_sagaId.Usage});");

// var rawIdentityType = _sagaId.VariableType.IsNullable()
// ? _sagaId.VariableType.GetInnerTypeFromNullable()
// : _sagaId.VariableType;
//
// if (_identityTypes.Contains(rawIdentityType))
// {
//
// }
// else
// {
// var valueType = ValueTypeInfo.ForType(rawIdentityType);
// writer.Write(
// $"var {_batchQueryItem.Usage} = {_batchQuery!.Usage}.Load<{Saga.VariableType.FullNameInCode()}>({_sagaId.Usage}.{valueType.ValueProperty.Name});");
// }


}

public void EnlistInBatchQuery(Variable batchQuery)
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Wolverine.Marten/Wolverine.Marten.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ProjectReference Include="..\Wolverine.Postgresql\Wolverine.Postgresql.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Marten" Version="8.15.0" />
<PackageReference Include="Marten" Version="8.15.4" />
</ItemGroup>
<Import Project="../../../Analysis.Build.props" />
</Project>
Loading