Design - how do I handle events on saved objects when they change state?
Maybe a stupid question, but I'm a novice developer.
Suppose in a data-driven CRM'ish application I have a customer type that can go through multiple phases - that is, phases 1-5.
As the client phase changes, events should be triggered. Example. When a customer enters phase 3 of phase 2, an email is sent, some lists are updated and some calculations are done.
I am guessing that the client state change may be the result of the application user manually updating the client through some kind of GUI.
So, I'm wondering - should I handle this by asserting that there is only one way to update the state of a client phase, and then ensuring that whenever that activity completes, a list of activities is executed?
In my mind (and in script) this would mean getting the client from the relational database, updating the phase field, saving the client back and then always reacting to that action, disabling any actions logged based on what the particular phase change is. However, I'm not sure if it would be wise if I wanted to do a group phase of 10,000 clients change.
Any thoughts at all on this? I'm really just looking for any input - let's say I have absolutely no idea.
source to share
I think OK has separate features, one for one client phase change and another for batch change. The latter will or will not perform additional actions if necessary. It could also do this in a more efficient way, or even queue up activities or part of them, such as send email, for background processing if additional activities are lengthy and the phase change needs to be completed in a timely manner.
Another problem arises when the phase change is due to some possibly difficult condition, rather than a manual phase change. Then, you should intercept the conditional check somewhere in your business logic, low enough to catch all update operations affecting the phase. But, as you wrote, this is not the case, since in your situation the phase change is done manually.
source to share
In many cases it is better to have an explicit function in your business logic to change the phase. This should be the only way to change the phase, and it's obvious. Like "ChangeCustomerPhase" (customer client, Phase newPhase), it makes it easier to process to track changes if everything can be changed freely.
source to share