Particular / NServiceBus

Build, version, and monitor better microservices with the most powerful service platform for .NET
https://particular.net/nservicebus/
Other
2.09k stars 647 forks source link

Sagas don't call constructor to run ConfigureHowToFindSaga #6270

Open lillo42 opened 2 years ago

lillo42 commented 2 years ago

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?

dvdstelt commented 2 years ago

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.

lillo42 commented 2 years ago

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

dvdstelt commented 2 years ago

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?

lillo42 commented 2 years ago

@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

dvdstelt commented 2 years ago

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?