I'm struggling in solving this architectural problem, Our system is using NService Bus, and Implementing DDD with EventSourcing using NEventStore and NES. The client application is WPF
I still can't decide what is the best practice to update the UI, for example: I have a UI to create (Batch {Id, StartDate, EndDate ,etc..}), after the user clicks save, I'm sending a command (CreateBatch),
Bus.Send<CreateBatch> (batch => {batch.Id = Guid.NewGuid(); .... etc });
Now,
Option #1
Shall I register for a reply as follows:
private void btnSave_Click(object sender,EventsArg e){
Bus.Send<CreateBatch> (batch => {batch.Id = Guid.NewGuid(); .... etc })
.Register<int>(c=> {
MessageBox.Show("Command Succeded");
Close();});
}
and at server side:
public void Hanlde(CreateBatch cmd){
//initiate aggregate and save it.
Bus.Return( /*What ?? Success Code?*/);
}
In this case, how to handle errors? (Validation errors, for example there is already a batch starts in the same date ?), since you can only return int or string !!
Option#2:
Send the command, close the window, fake that the record is added in the main grid, until user refresh the grid and get the real one from ReadModel database, or discover that the record is not added yet with no clue about what happened !!
private void btnSave_Click(object sender,EventsArg e){
Bus.Send<CreateBatch> (batch => {batch.Id = Guid.NewGuid(); .... etc });
Close();
}
Option#3
Send the command, close the window, fake that the record is added in the main grid but mark it as (In progress), wait for (BatchCreated) event to arrive, check that it is the same batch id we sent before, and then mark the record as persisted in the grid.
private void btnSave_Click(object sender,EventsArg e){
Bus.Send<CreateBatch> (batch => {batch.Id = Guid.NewGuid(); .... etc });
Close();
}
public class BatchHandler : IHandleMessages<BatchCreated>
{
public void Handle(BatchCreated evnt)
{
if(SomeCachForSentIDS.Contains(evnt.BatchId)
///Code to refresh the row in the grid and reflect that the command succeeded.
}
}
If you have any better options, or have suggestions about the mentioned options, or even some links about how to handle such situation, I would be grateful.
Thank you.