I am playing around with CQRS and the MediatR library, trying to learn some of the best practices. I've found hard to understand how to split all business logic into commands and queries.
Here it is an example:
I have an Address
entity and I already created two commands and one query: InsertAddressCommand
, UpdateAddressCommand
and GetAddressByUserIdQuery
.
There are endpoints in the controller in charge of execute the Insert, the Update or in charge of returning Addresses by user id.
There is also an endpoint named UpdateOrInsertAddress
which reuses the 2 commands and 1 query: first to get the address by user id and based on the result to execute the insert or the update.
So far so good.
Now, there is another endpoint SubmitPurchaseOrder
which is receiving a bigger object. All the information related to a purchase is received at once. The shipping and billing addresses are part of the posted object.
Then I would like to reuse the logic inside UpdateOrInsertAddress
since all the logic is already there (and I was taught to reuse code); but since UpdateOrInsertAddress
is an endpoint, I cannot execute it from another endpoint (SubmitPurchaseOrder
).
So here it is the question: What is the best practice in this context?
Some ideas off the top of my head:
- Move the logic in
UpdateOrInsertAddress
endpoint to another method in the controller and reuse it from both UpdateOrInsertAddress
and SubmitPurchaseOrder
endpoints. In such case, I would be putting logic in the controller and I don't know if that is correct.
- Create another layer, maybe close to the one with all commands and queries, and write there this kind of shared logic which will be later consumed by the two endpoints.
- Forget about reuse, avoid sunk cost fallacy and just create a new
UpdateOrInsertAddress
command which will be used by the two endpoints. In such case, the "logic" of retrieving address by user id, updating or inserting will be duplicated in this command.
Have you come across a scenario like this? What is the best practice?
question from:
https://stackoverflow.com/questions/66054400/cqrs-and-shared-logic-in-controllers-level 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…