mfelicio / NDomain

.NET framework that simplifies software development using DDD, Event Sourcing and CQRS based architectures.
MIT License
54 stars 19 forks source link

About how to properly use the command bus? #5

Open nengc opened 7 years ago

nengc commented 7 years ago

image When I send two command like before,it can not store in mysql db,exception is about duplicate the primary key. But if i change the second command "SaleId" , it can not load the event. Is something wrong about my comprehension of the commandBus. And i used redis and mysql,the config is below image image

Can you give me some advice about it ,thanks

mfelicio commented 7 years ago

Hi nengc,

Let me start by apologizing for the fact that the existing samples haven't been updated and are lacking a proper app where you can see how everything is wired up.

Going back to your questions, I think we can see 3 possible issues there:

EventStore not being able to load events

I think what is missing in your configuration is registering the Sale aggregate on the EventSourcing setup, which makes the EventStore be able to deserialize the events that the Sale aggregate cares about.

Try:

.EventSourcing(c => c.UseSqlServerStorage(...).BindAggregate<Sale>())

Going forward, I will make the BindAggregate registrations optional, so by default NDomain will bind all aggregates that are known to the application.

CommandBus usage

Only problem I see in your usage is sending two different commands with the same Id. Ideally all commands should have a different command Id. In your example you're using "seller" as command Id for both commands, which can be a problem. For example, if the underlying message bus has duplicate message detection enabled it could mean the second command isn't even published.

Sql Server duplicate primary keys

It would be good if you could share the actual exception message with all details, but what I can guess that could be happening is concurrent updates to the same aggregate, where it's possible that two events are generated with the same sequenceId. When trying to insert them one will succeed and the other will fail with a duplicate primary key error. This is normal behaviour and is part of the optimistic concurrency detection logic. However, the eventstore should be throwing a ConcurrencyException, not a SqlException.

This was tested against SqlServer, not MySql, and the error codes thrown by MySql are different from SqlServer, therefore the current SqlEventStore implementation may not be handling MySql errors correctly, as it was tested only against SqlServer.

I'd appreciate if you could open a bug with some more details, and I'd appreciate even more if you could submit a pull request with a fix :)

Let me know if you run into any other issues.

nengc commented 7 years ago

Thanks, I got it . But I have three other puzzles about it . The first is that if i a Transaction contains many commands , how to store the events and rollback? The second is that where and how to handle the database CRUD , and if need extra implements. The third is that when i use webapi , how to await the result to response , now i use redis sub/pub to do it , any other good ideas ? Thanks again!