-
Notifications
You must be signed in to change notification settings - Fork 536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to implement business rules? #15
Comments
@MZybura, a simple approach: In a rehydration method (those that updates sate by playing all past events) just assign the name of the item from a past event to a field of the aggregate, and then, when handling a command just compare the new name to the field. That's it |
And of course, if a validation fails don't throw an exception. Just publish an event like "CouldNotUpdateInventoryItemName" |
Do you mean in "Apply" method? So I'm not sure how it should work. My understanding is that InventoryCommandHandler invokes "InventoryItem.ChangeName" method with new name as parameter. Do you suggest that "ChangeName" method should not be change, "InventoryItemRenamed" event published and business logic implemented in "Apply" method? How to get current state of the object to compare new name with current name? |
Its all about left folding (a high order function, similar to aggregate in C# Linq) from previous events. It is a core concept of event sourcing. You left fold, or apply from an initial state all events that happened to the aggregate, starting from the beginning. For instance:
|
But can't you get race conditions then? If two threads change the name, looking at the name "foo", only one of them is right. Do you have some kind of optimistic locking? |
There is optimistic locking built into this example see expected version On Thu, Feb 4, 2016 at 11:51 AM, Per Lundholm [email protected]
Studying for the Turing test |
It is right here |
Thanks! :) |
Another example ... a business rule to prevent removal of a given number of items from stock if there is enough left. To implement that, the entity/aggregateRoot could have a count property, or a way to get the counts from the change events. |
I kind of did that... just did sort of the opposite by instead of adding a Min Qty to the Remove, I added a MaxQty to the Check In instead 🤷♂️ |
I'm wondering how to implement business rule depends on entity current state. For example: "You can change the name of the InventoryItem only if new name is longer".
The text was updated successfully, but these errors were encountered: