Saga DI - do I use ISagaRepository?

198 views
Skip to first unread message

rob smith

unread,
Jun 19, 2017, 12:44:55 AM6/19/17
to masstransit-discuss
I am building successfully using the IConsumerFactory<T> extension point to run custom consumer factories for specific consumers.
The custom factories make decisions about which dependencies to inject based on message header and/or content where the dependencies all implement the same inteface.

My question is how do I go about doing the same thing with sagas. I have been looking through the code and I see ISagaRepository and it seems that
this is where I should be looking but I am struggling to understand how to achieve what I want. Can anyone provide some explanation and/or example of
this.

many thanks,
rob

Alexey Zimarev

unread,
Jun 20, 2017, 6:30:31 AM6/20/17
to masstransit-discuss
I am not sure I understand your question. Saga repository just persists saga state.

rob smith

unread,
Jun 21, 2017, 1:32:16 AM6/21/17
to masstransit-discuss
Yes, I know I am confused on this and mentioning ISagaRepository is just a reflection of that :)

So, I need to inject a repository conforming to an interface into a Saga via its constructor. Which implementation that gets injected is determined by content of the message or the message header. I think a fairly typical multi-tenant requirement? I do this effectively with (non-saga) consumers using an implementation of IConsumerFactory<T> but I am confused - as you see!. In a consumer factory for a consumer I peek at the message and/or its header and use that information to resolve the consumer with the required service injected into its constructor.

I need to do the same thing with a saga so I am looking for an interface similar to IConsumerFactory<T> that I can implement where I can peek at the sagas incoming message and resolve the required service to inject it into the saga.

I could inject a lifetime scope into the saga and resolve the required service from that inside the saga, but I would prefer not to introduce the container into my sagas.

thanks again.

Alexey Zimarev

unread,
Jun 21, 2017, 2:04:25 AM6/21/17
to masstransit-discuss
Hm, Saga state machines are singletons, there is no point injecting anything there. You have only one instance for all your messages. The only thing that is different per message is the saga state instance but this is just a DTO-style object.

Chris Patterson

unread,
Jun 21, 2017, 9:29:47 AM6/21/17
to masstrans...@googlegroups.com
Are you using Automatonymous (state machine sagas)? If so, there is a way to create your own activities and allow the container to resolve dependencies for those activities.

The activity itself:

How it's called:

How it's registered:

The magic wiring how it works:


On Tue, Jun 20, 2017 at 11:04 PM, Alexey Zimarev <azim...@gmail.com> wrote:
Hm, Saga state machines are singletons, there is no point injecting anything there. You have only one instance for all your messages. The only thing that is different per message is the saga state instance but this is just a DTO-style object.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstransit-discuss@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/masstransit-discuss/5de4c55f-9ffa-4dd8-8eaf-5ae3ef2d14c7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

rob smith

unread,
Jun 22, 2017, 3:48:03 AM6/22/17
to masstransit-discuss
OK, thanks to both Alexey who gave me a duh! moment realizing I was thinking about sagas a little incorrectly.

And thanks to Chris - creating my own automatonymous Activities, this is great! So I am working with this.
MassTransit is just fantastic guys. Thanks for all the continuing effort.



On Wednesday, June 21, 2017 at 4:29:47 PM UTC+3, Chris Patterson wrote:
On Tue, Jun 20, 2017 at 11:04 PM, Alexey Zimarev <azim...@gmail.com> wrote:
Hm, Saga state machines are singletons, there is no point injecting anything there. You have only one instance for all your messages. The only thing that is different per message is the saga state instance but this is just a DTO-style object.

--
You received this message because you are subscribed to the Google Groups "masstransit-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to masstransit-discuss+unsub...@googlegroups.com.
To post to this group, send email to masstrans...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages