MassTransit / Automatonymous

A state machine library for .Net - 100% code - No doodleware
Apache License 2.0
736 stars 117 forks source link

How to re-use services from multiple sagas? #66

Closed balintn22 closed 4 years ago

balintn22 commented 4 years ago

There's something I struggle with, but it may be just limited understanding, can you please advise. I have two sagas (say, CreateFoo and UpdateFoo). There's a service (BarService) that both sagas would use to fetch - say - a list of available Bars. My naive thinking would make me implement something like BarService.GetBars(), responding to a message called GetBars. However, when I register outbound message delivery (message types to queues) I can only send the GetBars message to one of the queues. (CreateFoo and UpdateFoo are implemented in the same process). It also means I have to implement BarService.GetBars() twice, to respond to the same message from the two different sagas.

Similarly, the response message (say, GetBarsResult) can only be consumed by a single saga. So BarService has to be aware where the request came from and send the response to the mathing response queue.

So the current understanding is that I have to create GetBars and GetBarsResult twice, in different namespaces for each saga.

Is this a correct understanding or am I missing something? Is it possible to re-use the same message type for both sagas somehow? I understand it would require something like correlating messages to multiple sagas somehow. Is it possible?

phatboyg commented 4 years ago

I don't entirely understand what you're asking. Using the request client, a ResponseAddress header is added so that the requestor can be responded to by the service.

If you need to implement the same behavior in two sagas, I'd suggest creating an activity that is called by both state machines.

If you figured this out, great. If you haven't, feel free to ask on Discord (as this isn't an issue with Automatonymous).

rsanhuez commented 4 years ago

I don't entirely understand what you're asking. Using the request client, a ResponseAddress header is added so that the requestor can be responded to by the service.

If you need to implement the same behavior in two sagas, I'd suggest creating an activity that is called by both state machines.

If you figured this out, great. If you haven't, feel free to ask on Discord (as this isn't an issue with Automatonymous).

I have the same problem. I want to reuse some activities on two different state machines. The problem is that the activities are coupled with the "instance" and I am unable to reuse the same instance for two different state machines.

phatboyg commented 4 years ago

Oh, yeah, that's true. So you'd probably want to put the behavior in a dependency and call it from the activity.