Skip to content

Commit

Permalink
Fixing codegen issues for EF Core sagas. Closes GH-252
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy D. Miller authored and Jeremy D. Miller committed Mar 17, 2023
1 parent dbe4880 commit b74b38f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
69 changes: 69 additions & 0 deletions src/Persistence/PersistenceTests/EFCore/Bug_252_codegen_issue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using IntegrationTests;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weasel.Core;
using Weasel.SqlServer;
using Weasel.SqlServer.Tables;
using Wolverine;
using Wolverine.EntityFrameworkCore;
using Wolverine.SqlServer;
using Wolverine.Tracking;
using Xunit;

namespace PersistenceTests.EFCore;

[Collection("sqlserver")]
public class Bug_252_codegen_issue
{
[Fact]
public async Task use_the_saga_type_to_determine_the_correct_DbContext_type()
{
using var host = await Host.CreateDefaultBuilder()
.UseWolverine(opt =>
{
opt.Services.AddDbContextWithWolverineIntegration<AppDbContext>(o =>
{
o.UseSqlServer(Servers.SqlServerConnectionString);
});

opt.PersistMessagesWithSqlServer(Servers.SqlServerConnectionString);
opt.UseEntityFrameworkCoreTransactions();
opt.Policies.UseDurableLocalQueues();
opt.Policies.AutoApplyTransactions();
}).StartAsync();

var table = new Table("OrderSagas");
table.AddColumn<Guid>("id").AsPrimaryKey();
await using var conn = new SqlConnection(Servers.SqlServerConnectionString);
await conn.OpenAsync();

var migration = await SchemaMigration.DetermineAsync(conn, table);
await new SqlServerMigrator().ApplyAllAsync(conn, migration, AutoCreate.All);

var dbContext = host.Services.GetRequiredService<AppDbContext>();
await dbContext.Database.EnsureCreatedAsync();

await host.InvokeMessageAndWaitAsync(new OrderCreated(Guid.NewGuid()));
}
}

public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

public DbSet<ProcessOrderSaga> OrderSagas { get; set; }
}

public record OrderCreated(Guid Id);

public class ProcessOrderSaga : Saga
{
public Guid Id { get; set; }

public void Start(OrderCreated order)
{
Id = order.Id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public Frame CommitUnitOfWorkFrame(Variable saga, IContainer container)
var method =
dbContextType.GetMethod(nameof(DbContext.SaveChangesAsync), new[] { typeof(CancellationToken) });

return new MethodCall(dbContextType, method);
var call = new MethodCall(dbContextType, method);
call.ReturnVariable.OverrideName(call.ReturnVariable.Usage + "1");

return call;
}

public Frame DetermineUpdateFrame(Variable saga, IContainer container)
Expand All @@ -76,9 +79,6 @@ public void ApplyTransactionSupport(IChain chain, IContainer container)
chain.Middleware.Insert(0, new EnrollDbContextInTransaction(dbType));
}




var saveChangesAsync =
dbType.GetMethod(nameof(DbContext.SaveChangesAsync), new[] { typeof(CancellationToken) });

Expand Down Expand Up @@ -147,8 +147,13 @@ internal Type DetermineDbContextType(Type entityType, IContainer container)
return contextType;
}

public static Type DetermineDbContextType(IChain chain, IContainer container)
public Type DetermineDbContextType(IChain chain, IContainer container)
{
if (chain is SagaChain saga)
{
return DetermineDbContextType(saga.SagaType, container);
}

var contextTypes = chain.ServiceDependencies(container).Where(x => x.CanBeCastTo<DbContext>()).ToArray();

if (contextTypes.Length == 0)
Expand Down

0 comments on commit b74b38f

Please sign in to comment.