Question

I am refactoring an old procedural PHP website into a tasty OOP application with a light sprinkling of Domain Driven Design for added flavour.

I keep stumbling upon cases where I have a need for classes that can have subclasses which are either entities or value objects.

An url object, for example. There are a zillion urls out there and so they all cannot really be entities. But some are very special urls, like my home page. That is an entity.

Another example is, say, a 'configuration object'. I'd like some configurations to have identities so i can create 'presets' and administer them via an online control panel. For those a finder/repository is needed to find them and ORM is needed to manage their lifetimes. But, for others 'not-presets' (of the same class hierarchy) I'd like to be able to load them up with data that has been customised on the fly and does not need to be persisted.

I am envisaging a lot of :

class factory { 
 reconstitute($rawdata) {
  if (raw data has identity)
   load up and return entity version of the class 
  else
   load up and return anonymous/value object version of the class

It all seems a bit odd.

Is there any pattern out there that discusses the best way to handle this issue?

Was it helpful?

Solution

I'm not sure I totally understand your scenerio but... does that really matter? In my experience with EFs/ORMs the best way (that I can think of) to do what you are wanting to do is to let your entity class decide whether or not to load/persist itself from/to a database based on business rules defined in the class.

$url = new URLClass('KEY_DATA') // returns loaded object url if key if found in database
$url = new URLClass() // returns new url object
$url = new URLClass('', '110011000110010001000011101010010100') // returns new url with data loaded from raw data

Not sure if that really helps you out or if it even applies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top