On 15 Maj, 11:51, RichardETVS <PETI...@e-i.com> wrote:
> While encapsulation is a general rule, and a good one, there can be
> case where it is not the best, in my humble opinion.
>
> Take an object database like db4o, by example (www.dbf4.com) . You
> save an object with something like
> "anObjectDataBaseManager.Save(TheObjectIwantToSave ). And it is saved,
> even its private fields. To fully load an object, you can use a syntax
> like "ObjectContainer.Activate(TheObjectIwantToFullyLoa d,
> int.MaxValue)".
>
> So, in those cases, the encapsulation is broken. For a data layer, I
> had to something like that. The BO objects ask to the data layer to
> save and update them. I did not want to use .net reflection, so I used
> the Separate Interface Pattern, and with an explicit interface, my BO
> expose all their private fields to the data layer.
>
> I have to amit my solution has a problem. The user interface layer, in
> C#, asks a reference to the interface module, it seems that in VB it
> is not the case. So, if a developer really want it, he can use the
> explicit interface mechanism to acces private BO fields.
>
> Richard
Hej, Richard,
While I fully agree that there is always a case for breaking a rule in
special circumstances (even encapsulation), could I just note a humble
alternative to your approach, though I lack your insight into the
problem?
Firstly, encapsulation can help insulate a class from changes in
another class.
In the solution you have above, I presume that the following line
means: create an instance of TheObjectIwantToFullyLoad and set its
MaxValue accordingly:
"ObjectContainer.Activate(TheObjectIwantToFullyLoa d, int.MaxValue)"
If this is the case, then we can consider what happens when
TheObjectIwantToFullyLoad gets a new field variable, let's call it:
String name.
Now, both TheObjectIwantToFullyLoad must change (to add this new
variable) and - more importantly - ObjectContainer must change, as it
must now call:
"ObjectContainer.Activate(TheObjectIwantToFullyLoa d, int.MaxValue,
string.name)"
This, we note, is the price of breaking encapsulation.
The question is: could there be a way to respect encapsulation so
that
ObjectContainer isn't affected by any changes to the internals of
TheObjectIwantToFullyLoad?
An answer could be to have TheObjectIwantToFullyLoad (and any
serialisable object) responsible for its own data serialisation. It
could have a method save(BitStream stream). This method is called by
ObjectContainer and it passes in the stream that will be written to
the file system.
It is then up to TheObjectIwantToFullyLoad to convert all its private
data to a bit-stream representation and write this data to the bit-
stream.
The reverse is true when reading a bit-stream from the file system:
TheObjectIwantToFullyLoad itself will be resonsible for inspecting
the bit-stream and converting it back to an int and a string value.
Now, changes to TheObjectIwantToFullyLoad do not affect the
ObjectContainer.
..ed
--
www.EdmundKirwan.com - Home of The Fractal Class Composition.
Download Fractality, free Java code analyzer:
http://www.EdmundKirwan.com/servlet/...c-page130.html