NServiceBus icon indicating copy to clipboard operation
NServiceBus copied to clipboard

Sagas don't call constructor to run ConfigureHowToFindSaga

Open lillo42 opened this issue 3 years ago • 5 comments

Hi,

I'm testing source code generation with NServiceBus, so I have a constructor where I receive some parameter that I want to use ConfigureHowToFindSaga but those parameter is null, apparently when ConfigureHowToFindSaga the constructor isn't been call, is that correct?

lillo42 avatar Feb 16 '22 21:02 lillo42

Hi @lillo42, I'm not sure I understand the question.

Sagas are invoked using a dependency injection container. Is that what you're referring to? Then any parameter in the constructor of the saga should theoretically also be injected by the DI container, if it is properly regsitered.

dvdstelt avatar Feb 21 '22 13:02 dvdstelt

This code throw null reference, if I comment the Console.WriteLine("{0}", _depedency.IsEnable); on ConfigureHowToFindSaga works:

public class SampleSaga : Saga<SampleData>, IAmStartedByMessages<SomeMessage>
{
    private readonly SomeDependency _depedency;

    public SampleSaga(SomeDependency depedency)
    {
        _depedency = depedency;
    }

    protected override void ConfigureHowToFindSaga(SagaPropertyMapper<SampleData> mapper)
    {
        Console.WriteLine("{0}", _depedency.IsEnable);
        mapper.ConfigureMapping<SomeMessage>(x => x.Id)
            .ToSaga(x => x.Id);
    }

    public async Task Handle(SomeMessage message, IMessageHandlerContext context)
    {
        Console.WriteLine("{0}", _depedency.IsEnable);
    }
}

sample.zip

lillo42 avatar Feb 21 '22 17:02 lillo42

I understand now what you're looking for. The ConfigureHowToFindSaga is intended to only configure the mappings. It should not be used for anything else. If you're doing conditional mapping, the saga is probably already getting too complex.

Is there a specific scenario you want to achieve that I might be able to help with?

dvdstelt avatar Feb 23 '22 09:02 dvdstelt

@dvdstelt I'm trying to integrate/playing with Automatonymous lib with NServiceBus, for that I'm using source code generator to generate the saga, so for ConfigureHowToFindSaga I want to use NServiceStateMachine instance(which one is singleton) to configure how to find saga.

A sample of a state machine https://github.com/lillo42/NServiceBus.Automatonymous/blob/code-improve/sample/SimpleStateMachine/OrderStateMachine.cs

lillo42 avatar Feb 26 '22 08:02 lillo42

Automatonymous seems to be a state machine, something a saga could be seen as, as well. It feels like you're overcomplicating things by combining those two, or I am lacking to fail what you're trying to achieve.

If you want a more complex way to find a specific saga, you can use the IFindSagas interface. I'm still failing to see what you're trying to achieve. If you're generating source code, can't you generate the mapping? I can investigate what you're trying to achieve with Automatonymous, but that would take me a while and I'm still not sure that would help me understand what you're trying to achieve?

dvdstelt avatar Mar 01 '22 16:03 dvdstelt