Ok so I have my CustomerMapper
mapping my Customer
domain object perfectly but today I was in the middle of some business logic in one of my domain objects which uses my Customer
object and I realized I needed to have some sort of
if( $customer->getNumActiveOrders() > self::MAX_NUM_ORDERS )
Currently my Customer
object does not have a $numActiveOrders
property so I would have to fetch the number of active orders for this customer only when the getNumActiveOrders()
method is called.
The confusion is how do I go about doing this without having any database logic in my Customer
domain object? Maybe inject a DAO into my Customer
object on creation and let it stop database logic leaking into my domain object? I do not really like the idea of having anything database related in my domain objects though so I am unsure about that one.
Maybe I should have a $numActiveOrders
property in my object and just set it when my DataMapper fetches the customer? I do not think I will need this value too much, maybe 3 times (if even) in the whole application so I do not want to be doing an extra query every time I create a Customer
How do people do this sort of task efficiently and without leaking database logic into domain objects?
Thanks.