简体   繁体   中英

Navigation properties in related MassTransit Sagas not working

I'm working on a generic batch component ( Publish message in Saga where type is known only runtime ) heavily inspired by Chris Pattersons sample https://github.com/MassTransit/Sample-Batch . However there seems to be an issue with the sample, the navigation properties on BatchState and BatchJobState are not working.

In the BatchStateMachine when a BatchJobDone event is fired i need to detect if it's the last BatchJob in order to fire a new event.

public class BatchState :
    SagaStateMachineInstance
{
    ...
    
    // Navigation Properties
    public List<BatchJobState> Jobs { get; set; } = new List<BatchJobState>();
    public Guid CorrelationId { get; set; }
}
public class BatchJobState :
    SagaStateMachineInstance
{
    ...

    // Navigation Properties
    public BatchState Batch { get; set; }
    public Guid CorrelationId { get; set; }
}

The Batch property on BatchJobState is always null and the Jobs property on BatchState is always Count = 0

They're configured like this:

class BatchStateEntityConfiguration :
    IEntityTypeConfiguration<BatchState>
{
    public void Configure(EntityTypeBuilder<BatchState> builder)
    {
        builder.HasKey(c => c.CorrelationId);

        builder.Property(c => c.CorrelationId)
            .ValueGeneratedNever()
            .HasColumnName("BatchId");

        builder.Property(c => c.CurrentState).IsRequired();

        builder.Property(c => c.Action)
            .HasConversion(new EnumToStringConverter<BatchAction>());

        builder.Property(c => c.UnprocessedOrderIds)
            .HasConversion(new JsonValueConverter<Stack<Guid>>())
            .Metadata.SetValueComparer(new JsonValueComparer<Stack<Guid>>());

        builder.Property(c => c.ProcessingOrderIds)
            .HasConversion(new JsonValueConverter<Dictionary<Guid, Guid>>())
            .Metadata.SetValueComparer(new JsonValueComparer<Dictionary<Guid, Guid>>());
    }
}
class JobStateEntityConfiguration :
    IEntityTypeConfiguration<BatchJobState>
{
    public void Configure(EntityTypeBuilder<BatchJobState> builder)
    {
        builder.HasKey(c => c.CorrelationId);

        builder.Property(c => c.CorrelationId)
            .ValueGeneratedNever()
            .HasColumnName("BatchJobId");

        builder.Property(c => c.CurrentState).IsRequired();

        builder.Property(c => c.Action)
            .HasConversion(new EnumToStringConverter<BatchAction>());
    }
}

For navigation properties to show you would need to use Eager Loading via.Include(...). This can be added to repositories using the query customization.

Inside the.AddSagaStateMachine<>().EntityFrameworkRepository(r => )

you would add:

r.CustomizeQuery(x => x.Include(y => y.Jobs));

And

r.CustomizeQuery(x => x.Include(y => y.Batch));

to the appropriate state machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM