views:

39

answers:

1

Hi

In this article Ayende describes how to map a single domain model to multiple physical data models. Is it possible to extend this principle such that the mapping can chosen dynamically?

So for example, imagine we had an entity that could be written to the same physical schema in three ways depending on its current status, and lets assume that regardless of status each entity had a unique identifier.

One solution would be to represent the entity in its different states with three separate classes: one for each mapping. Then the entity could be loaded and in order to change its state the entity could be mapped to a class representing one of its other states and then saved back to the schema, making use of a different mapping.

I was wondering if it is at all possible to have the same entity represented by one class that held a status flag (kind of like a discriminator), and any save to the schema would choose the appropriate mapping based on the value of the status flag.

Hopefully that made sense!

Many thanks.

A: 

Even if it were possible, IMHO this is not a good idea. It's basically the same thing asked in this question, and I agree with the answers posted there. Basically, keep it simple, and model your states within a single mapped class.

Mauricio Scheffer
Thanks for your reply. In my opinion if it were possible then it would be cleaner. Just to clarify: I am not changing an entity's type (something I will need to do if it isn't possible), I am changing how it is persisted. This is something the object model should not care about and needing to have multiple classes to represent the same entity pollutes the object model.
Nigel
I stand by my answer. You are representing different states of the same thing with different classes. IMO that *is* effectively changing the entity type depending on inner state. I don't recommend that, especially because you have a simpler solution which is just representing state with normal properties or even with some `<any>` relationship.
Mauricio Scheffer
Downvoters: care to express your opinions/alternatives/solutions?
Mauricio Scheffer