mercredi 6 avril 2022

Rules Pattern on Object Change

I have a system that lets people create and work with Orders. Orders can have multiple Steps. The Order can have a status of New, InProgress, Done, and Cancelled.

There are a bunch of business rules on what can and can't be done. Examples of rules are "can only add/remove Steps to an Order if the Order is New", "can't move an Order to Cancelled if any Step has a Cost>0", and "the Order must have Approved=true to move from New to InProgress".

I'm trying to find a good way to enforce these rules, other than several if statements splattered all around the code. I saw the Rules design pattern, but everything I can find on that applies to a single object state, and not a change in the object.

My current approach is this. I have a bunch of IRules. Each one has a Validate(Order source, Order destination) method that validates that the transition is allowed by its rule. Each endpoint in my API loads the current Order, creates a new Order with the change made, then calls a method that runs all the rules.

Am I going about this wrong? Any suggestions? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire