G
Guest
I have an object that has a fairly complex construction sequence, so I have
written a dedicated "factory" class that invokes the constructor of my object
class (which does nothing but instantiate the object and set default
blank/null values), and then does all the Db access and number crunching to
populate the new object. The factory returns the fully populated object to
the caller. All the fields in the object are private, but have public
properties with both Get and Set methods. If the Set methods weren't public,
the factory couldn't populate them.
Now I would like my factory class to create either a Read-Only or a
Read/Write version of my object (they're all Read/Write today). The object
is remotable and I want to be able to control whether the object is
updatable.
Any ideas?
I thought of this...
1) Make a version of the class where all the public properties have only
"Get" code associated with them (i.e. all properties of created objects can
only be read). Make all the properties in this class "virtual".
2) Subclass the "read only" class and overrride the properties to contain
both Get and Set code. The Get methods just reference base.Get so there
isn't any code duplication (haven't tried this don't know if this is the
right syntax). The Set code updates the appropriate private field data.
3) Write a "CopyToReadOnly" method in the subclass that a) instantiates a
"read only" object (i.e. call the default constructor on the superclass), b)
copies all the values from the instance fields in the read/write object
directly to the same fields in the new "read-only" object c) returns the
"read only" version of the original "read/write" object. So to get a
"read-only" object I a) tell the factory to make a read/write object, b)
tell that object to make a "read-only" copy of itself, and c) return the
"read-only" version to the caller.
This seems much too complex, and I don't like the performance hit of having
to instantiate the object twice, once as read/write and again as read-only.
Somebody must know how to do this better.
Thanks.
BBM
written a dedicated "factory" class that invokes the constructor of my object
class (which does nothing but instantiate the object and set default
blank/null values), and then does all the Db access and number crunching to
populate the new object. The factory returns the fully populated object to
the caller. All the fields in the object are private, but have public
properties with both Get and Set methods. If the Set methods weren't public,
the factory couldn't populate them.
Now I would like my factory class to create either a Read-Only or a
Read/Write version of my object (they're all Read/Write today). The object
is remotable and I want to be able to control whether the object is
updatable.
Any ideas?
I thought of this...
1) Make a version of the class where all the public properties have only
"Get" code associated with them (i.e. all properties of created objects can
only be read). Make all the properties in this class "virtual".
2) Subclass the "read only" class and overrride the properties to contain
both Get and Set code. The Get methods just reference base.Get so there
isn't any code duplication (haven't tried this don't know if this is the
right syntax). The Set code updates the appropriate private field data.
3) Write a "CopyToReadOnly" method in the subclass that a) instantiates a
"read only" object (i.e. call the default constructor on the superclass), b)
copies all the values from the instance fields in the read/write object
directly to the same fields in the new "read-only" object c) returns the
"read only" version of the original "read/write" object. So to get a
"read-only" object I a) tell the factory to make a read/write object, b)
tell that object to make a "read-only" copy of itself, and c) return the
"read-only" version to the caller.
This seems much too complex, and I don't like the performance hit of having
to instantiate the object twice, once as read/write and again as read-only.
Somebody must know how to do this better.
Thanks.
BBM