Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton NServiceBus Saga

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.
    }
}
like image 365
izildur Avatar asked Apr 20 '26 01:04

izildur


1 Answers

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.

like image 173
Mike Minutillo Avatar answered Apr 21 '26 15:04

Mike Minutillo