NB: Kind of abstract and non-idiomatic post.
Anyone tried the design pattern of containing purity within your program except at the top level? Say, instead of passing around a model which you can modify and update the database with, your methods will return immutable transaction objects which you collect and then execute at the top. It’s a little bit like the IO monad in Haskell, I guess? Not sure if it has a name.
Example class (for database changes, but same can be done for file IO and echo, etc):
class Transaction {
private $changes = []; // column => new value
public function __construct($changes) { ... }
public function execute($dbconnection) { ... }
}
Use-case: As usual, testability, mockability, composability, being able to do dry-runs.
Con: If the code depends on changes in the database executed earlier, you must pass around the “new state” in form of transaction history and basically dry-run it to get the new state? Also, for reading data it can be hard to predict beforehand what a service class will need, depending on its logic; not really practical to inject a whole database or file as read-only.
real estate digital marketing company runwal project in dombivali