繁体   English   中英

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

[英]Navigation properties in related MassTransit Sagas not working

我正在研究一个通用批处理组件( 在 Saga 中发布消息,其中类型仅在运行时已知)受到 Chris Pattersons 示例https://github.com/MassTransit/Sample-Batch的极大启发。 但是,示例似乎存在问题, BatchStateBatchJobState上的导航属性不起作用。

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; }
}

BatchJobState上的Batch属性始终为null并且BatchState上的Jobs属性始终为Count = 0

它们的配置如下:

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>());
    }
}

要显示导航属性,您需要使用 Eager Loading via.Include(...)。 这可以使用查询自定义添加到存储库。

在.AddSagaStateMachine<>().EntityFrameworkRepository(r =>)里面

你会添加:

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

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

到相应的 state 机器。

暂无
暂无

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

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