如何确定基于Saga的类,以便将其从SQL数据库(EF Core)中删除? [英] How do I finalize a class based Saga, so that it is deleted from the SQL database (EF Core)?

查看:101
本文介绍了如何确定基于Saga的类,以便将其从SQL数据库(EF Core)中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试采用大众运输系统时,我正在尝试学习更多有关大众运输系统的信息.现在,我在下面有了基于Saga的类,该类可以按预期工作:

I am trying to learn more about Mass Transit as we are thinking about adopting it. I now have the class based Saga below, which works as expected:

public class EchoSaga : ISaga,
        InitiatedBy<TextEntered>,
        Orchestrates<TextEchoStart>,
        Orchestrates<EchoEnd>
    {
        public Guid CorrelationId { get; set; }
        public string CurrentState { get; set; }
        public string Text { get; set; }

        public Task Consume(ConsumeContext<TextEntered> context)
        {
            CurrentState = "Entered";
            Text = context.Message.Text;
            return Task.CompletedTask;
        }

        public Task Consume(ConsumeContext<TextEchoStart> context)
        {
            CurrentState = "Start";
            Text = context.Message.Text;
            return Task.CompletedTask;
        }

        public Task Consume(ConsumeContext<EchoEnd> context)
        {
            CurrentState = "End";
            Text = context.Message.Text;
            return Task.CompletedTask;
        }
    }

基于类的Saga与状态机Saga不同,在Saga部分的文档中对此进行了更多描述:

A class based Saga is different to a state machine Saga and is described more in the docs here in the Saga section: http://masstransit-project.com/MassTransit/understand/key-ideas.html.

如何在消耗EchoEnd之后将Saga标记为已完成,以便将Saga从数据库中删除(我已经设置了存储库)?如果我正在使用状态机Saga,则可以执行以下操作:

How do I mark the Saga as finalized after EchoEnd is consumed so it is deleted from the database (I have already setup the repository)? If I was using a state machine Saga, then I could do this:

.Finalize()
.SetCompletedWhenFinalized()

我该如何使用基于Saga的类?

How do I do this with a class based Saga?

我意识到我在这里可能会倒退一点,但是我正在尝试学习大众运输的开始方式以及现在的位置,看看它是否符合我们的要求.到目前为止非常满意.

I realise I may be going backwards a bit here, however I am trying to learn how Mass Transit started and where it is now to see if it meets our requirements. Very pleased with it so far.

推荐答案

如果将ConsumeContext强制转换为SagaConsumeContext<TSaga, TMessage>,则存在SetCompleted方法,该方法表明传奇已完成,可以从存储库中删除.

If you cast the ConsumeContext to SagaConsumeContext<TSaga, TMessage>, there is a SetCompleted method which signals the saga is complete and can be removed from the repository.

如果直接投射不起作用(由于在管道中使用代理),则可能需要使用context.GetPayload<SagaConsumeContext<TSaga, TMessage>>().

You may need to use context.GetPayload<SagaConsumeContext<TSaga, TMessage>>() if casting directly doesn't work (due to proxy use in the pipeline).

这篇关于如何确定基于Saga的类,以便将其从SQL数据库(EF Core)中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆