views:

40

answers:

1

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?

A: 

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.

Dusty Lau
Yes, thanks. That was helpful. I have been obsessed with making my entity classes throw out any attempts to create them without an id (I could not see how they might work with an 'Identity Map' (PoEAA) otherwise). So, I guess you are saying to loosen up on that and let the object exist without an identity - which would solve the current issue.
JW
p.s. what is an 'EF'?
JW
EF = Entity Framework
Dusty Lau
And yes that was what I was saying. I often create an instance of a persistable class with no intention of persisting the data. Some would argue that it introduces unnessesary overhead (which is true) but sometimes it doesn't really matter that much.I have also worked with some frameworks that used a persistance layer and dumb objects. You would not add any extra overhead with that approach and your problem would still be solved.
Dusty Lau