Using a state object to store object property values
In an old post I dealt with a possible implementation of a BaseEntity class that stores all properties in a State object (based on a dictionary to store properties). Technically speaking, this solution have no drawbacks respect using field variables to store properties values, but it can give a lot of benefit. If you see the object from the outside, it presents a bunch of properties and thanks to encapsulation you can use the technique you prefer to store the real data inside the object. This approach has some drawbacks, first of all accessing the value of a property can be slower, because you need to find the value inside a dictionary instead of reading directly from a memory location (the field value). Another drawback is that I use the property names as dictionary keys, so you waste more memory to store the keys of the state dictionary, but you can limit this problem using string interning.
This model has indeed some advantages respect using field variables, first of all you can easily find the value of every property from the String representation without the need of reflection. One of the first advantage is the ability to Deep Clone an object with a simple routine.
|
|
As you can verify from the code I simply create another instance of the entity, then simply assign a clone of the original status to the new entity.
|
|
Clone method is quite simple and it is not tested in all scenario (I’ve tested for basic classes) but it works quite well and solves easily the needs to deep clone an object. I’m not sure that in strong OOP oriented software Deep Cloning is a needed thing, but when you work with ORM and User Type (more in future posts), it is quite useful.
Gian Maria