简体   繁体   English

相关 MassTransit Sagas 中的导航属性不起作用

[英]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 .我正在研究一个通用批处理组件( 在 Saga 中发布消息,其中类型仅在运行时已知)受到 Chris Pattersons 示例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.但是,示例似乎存在问题, BatchStateBatchJobState上的导航属性不起作用。

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.BatchStateMachine ,当触发BatchJobDone事件时,我需要检测它是否是最后一个 BatchJob 才能触发新事件。

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 BatchJobState上的Batch属性始终为null并且BatchState上的Jobs属性始终为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(...).要显示导航属性,您需要使用 Eager Loading via.Include(...)。 This can be added to repositories using the query customization.这可以使用查询自定义添加到存储库。

Inside the.AddSagaStateMachine<>().EntityFrameworkRepository(r => )在.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.到相应的 state 机器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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