努力实施 IFindSagas

发布于 2024-12-20 21:41:03 字数 777 浏览 1 评论 0原文

我有一个持续一天的传奇。

收到消息后,我想找到活动的传奇并在 Handle(message) 方法中执行操作。

我没有 ConfigureHowToFindSagas 方法,因为我想要当前的方法(如果有)。所有收到的消息都会影响一个传奇,直到收到超时消息。当前的传奇完成,并在收到新消息后创建新的传奇。但根据我所读到的内容,我需要实现 IFindSagas 才能做到这一点,可能还需要实现我自己的 Saga 持久化器。

我需要关于从哪里开始以及这是否是正确的方法的建议。代码示例会很有帮助,因为我对使用界面还比较陌生。

   // fragment from Saga<PaymentSagaBase>
 public void Run()
        {

        ScheduleBatchIDForSession = Guid.NewGuid();

        // Message handlers aren't auto-subscribed in Saga scenarios so it needs to happen here.
        Bus.Subscribe<PaymentRequested>();
        Bus.Subscribe<PaymentCancelled>();

        Logger.Info(string.Format("Creating new Saga.");

        RequestUtcTimeout(DateTime.Now.AddHours(23), "End of batch");
    }

I have a saga that runs for a day.

Upon receiving a message, I want to find the active saga and do things in the Handle(message) method.

I don't have a ConfigureHowToFindSagas method as I want the current one, if there is one. All messages received will impact on one saga until the timeout message is received. The current saga completes and a new one is created upon receiving a new message. But from what I've read, I need to implement IFindSagas in order to do this and possibly my own Saga persister.

I need advice on where to start and also if this is the right way to go. Code examples would be helpful as I'm still relatively new to using interfaces.

   // fragment from Saga<PaymentSagaBase>
 public void Run()
        {

        ScheduleBatchIDForSession = Guid.NewGuid();

        // Message handlers aren't auto-subscribed in Saga scenarios so it needs to happen here.
        Bus.Subscribe<PaymentRequested>();
        Bus.Subscribe<PaymentCancelled>();

        Logger.Info(string.Format("Creating new Saga.");

        RequestUtcTimeout(DateTime.Now.AddHours(23), "End of batch");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-12-27 21:41:03

我想我只是前几天回答了类似的问题。如果您有更复杂的逻辑来查找现有的 saga,则实现 IFindSagas 接口,否则重写 ConfigureHowToFindSaga 方法就足够了。您需要做的就是确保配置映射到启动传奇的消息。有点像这样:

public class MySaga : Saga<MySagaData>, IAmStartedByMessages<Message1>
{
    public override void ConfigureHowToFindSaga()
    {
         ConfigureMapping<Message1>(s => s.SomeID, m => m.SomeID);
    }

    public void Handle(Message1 message)
    {
       if(Data.Id != Guid.Empty)
       {
           // handle existing saga
       }
       else // create new saga instance
       {
          this.Data.SomeID = message.SomeID;
          RequestUtcTimeout(DateTime.Now.AddHours(23), "End of batch");
       }

        // rest of the code to handle Message1
    }

    public override void Timeout(object state)
    {
        // some business action
    } 
}

HTH

I think I just answered a similar question the other day. If you have more complicated logic in order to find your existing saga, then implement IFindSagas interface, otherwise overriding the ConfigureHowToFindSaga method should be enough. All you need to do is make sure the configuration maps to the message that started the saga. Sort of like this:

public class MySaga : Saga<MySagaData>, IAmStartedByMessages<Message1>
{
    public override void ConfigureHowToFindSaga()
    {
         ConfigureMapping<Message1>(s => s.SomeID, m => m.SomeID);
    }

    public void Handle(Message1 message)
    {
       if(Data.Id != Guid.Empty)
       {
           // handle existing saga
       }
       else // create new saga instance
       {
          this.Data.SomeID = message.SomeID;
          RequestUtcTimeout(DateTime.Now.AddHours(23), "End of batch");
       }

        // rest of the code to handle Message1
    }

    public override void Timeout(object state)
    {
        // some business action
    } 
}

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文