charlessolar / Aggregates.NET

.NET event sourced domain driven design model via NServiceBus and GetEventStore

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for sending commands from message handlers

charlessolar opened this issue · comments

In NSB 6 I can't use request / response in message handlers - see Particular/NServiceBus.Callbacks#42

The "correct" way to do this from what I'm told is to setup a saga which can collect replies from normal Sends and handle timeouts / retries and such in itself.

It sounds good - and sounds like something we can include in the library so users won't have to deal with that logic themselves

I added a small extension IMessageHandleContext.LocalSaga to help with sending a series of commands from message handlers.

Usage is like so:

public void Handle(Commands.DoSomethingComplicated command, IMessageHandlerContext ctx) {
   var complicated = await ctx.For<Complicated>().Get(command.ComplicatedId);
   await ctx.LocalSaga(async bus => {
       var reply = await bus.Request<bool>(new Commands.StartSomething{ ComplicatedId = complicated.Id });
       complicated.Success = reply;
   });
}

awaiting ctx.LocalSaga will pause the command processing until the saga function is complete.

Working on the feature - syntax is going to be

var saga = ctx.Saga("blah")
.Command<Commands.Allocate>(x => {
  x.Id = "blah"
})
.Command<Commands.SetName>(x => {
  x.Id= "blah";
  x.Name = "Test";
})
.Command<Commands.Activate>(x => {
  x.Id = "blah";
})
.OnAbort<Commands.SetException>(x => {
  x.Id = "blah";
});
await saga.Start();

Under the covers we'll be running a normal NSB saga which will keep track of the commands already sent and accepted. If any command is rejected the abort commands will be run in order and the originating message will be sent to the error queue