Question

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");
    }
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top