3

What would be the correct way to have all the messages processed by a single saga?

I don't think I can not specify some message-to-saga correlation. Can I? I believe it would result in a "saga not found" error.

A naive way would be to have some constant ID in the saga, but that seems wrong.

class SomePolicy :
    Saga<SomePolicy.State>,
    IAmStartedByMessages<SomeEvent>
{
    internal class State : ContainSagaData
    {
        public int Id { get { return 1; } }
    }

    protected override void ConfigureHowToFindSaga(SagaPropertyMapper<State> mapper)
    {
        mapper
            .ConfigureMapping<SomeEvent>(message => message.MagicConstant)
            .ToSaga(saga => saga.Id);
    }

    public void Handle(SomeEvent message)
    {
        // Modify the saga state here.
    }
}
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
izildur
  • 33
  • 3
  • 1
    Why do you want this? – tom redfern Dec 22 '15 at 11:19
  • 1
    This looks correct (didn't run the code), what is your issue? What are you trying to do? – Sean Farmar Dec 22 '15 at 18:14
  • @TomRedfern let's say I want to aggregate some global information, such as how many times certain events occur in the system, I could use a singleton saga to receive all these events, increment some counter and perform an action when the counter reaches a certain threshold. – izildur Dec 22 '15 at 23:53
  • @SeanFarmar I'm really just looking for some sort of best practice, if there is any. It feels wrong that I have to correlate things that way, given that correlation should always give the same result. – izildur Dec 22 '15 at 23:56
  • But why do you need the correlation to give the same result? What's the functional reasoning behind this? What are you trying to accomplish? – Dennis van der Stelt Dec 23 '15 at 08:00
  • If you can explain your business use case it will help to suggest a good solution, at the moment I find it difficult to understand what you are trying to achieve... There is no one solution for all scenarios, and your question sounds like a special case (to me) – Sean Farmar Dec 23 '15 at 12:14

1 Answers1

2

Rather than override ConfigureHowToFindSaga you can supply an implementation of IFindSagas<T>.Using<M> which is used to find a saga of type T from a message of type M. Then just have it always return the same instance.

See Complex Saga Finding Logic for more details and some samples.

Mike Minutillo
  • 54,079
  • 14
  • 47
  • 41