I have an application that uses Doctrine ORM for entities. I try to push as much business logic into the entities as possible, following OO design. But I have run into a small problem. Some of the business logic requires variables that are defined in my external application configuration (because they sometimes need to be tweaked). How do I get these into my entities without violating OO principles? I don’t want to access my global configuration directly from my entities, and I don’t want to store these variables inside my entities and database either.
Here’s a mode concrete example. I use PHP with Doctrine ORM but the same kind of OO principles apply to Hibernate (JAVA), nHibernate (C#), etcetera.
My application has Products and Users. There are many different kinds of Users (real human users, automated robots, etcetera). They all need to work on the Product object. All the users share the same UserInterface which has a single method:
class UserInterface {
function canProcess(Product $product);
}
I have a ProcessService that mediates between the Products and the Users. It just iterates over all the Users attached to a Product until it finds one that is willing to process it at that time.
class ProcessService {
// Process a product
function process(Product $product) {
foreach ($this->getUsers() as $user) {
if ($user->canProcess($product)) {
...
}
}
}
The problem is that some of the users (in my case one of the robots) needs to have an external configuration variable in order to determine if it can process a product.
I don’t want to store that value in my entity and database (it’s not the right place) and I don’t want to access my global configuration object from inside the robot entity (violates OO). But, I also can’t pass it throught the canProcess() interface method because this variable is only applicable for that one type of robot, not for all the other kinds of users in my system. So, it has no place in the UserInterface either.
So, how do I get this configuration variable into my entity?
I solved it by using a
postLoadevent handler. The application configuration is passed to the event handler, and on thepostLoadevent I inject the configuration settings in the appropriateUserentities that need it.