 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dave White Guest
|
Posted: Tue Jan 18, 2005 11:44 pm Post subject: Object persistence and private fields |
|
|
I have a class that stores project data which is used throughout an
application. I am only accessing the fields using properties via getters
and setters, so the main code can't see the internal fields. However, some
of these properties need to be read-only for security, an example being
"LastTouched" - the app needs to read that to see when the data was last
changed, but I only want the FLastTouched field to be modified within the
class - when another field is modified. This works fine to this point, but
I'm not sure how to handle this in the persistence layer.
The data is stored in a Firebird database, and tranferred to a class
instance via a simple persistence layer located in a datamodule. The module
has a couple of methods, OpenProject(TProject) and SaveProject(TProject). I
pass an instance of the TProject class to the module, which then reads the
data from the database and fill the instance via the properties. The
problem is, it can't update the read only properties, and it can't see the
private fields because the data module and class declerations are in
different units (to allow for different persistence layers down the road).
Have I made a blatant design fault here, or if not, how do I get around this
problem?
For other classes that I've designed to this point, I've put the class
decleration in the same unit as the
data module - but tghis is messy, and exposes the data module to the rest
of the app, which I'd rather not do.
Can anybody point me in the right direction?
Thanks
Dave White
SpectraChrom Software
www.spectrachrom.com
|
|
| Back to top |
|
 |
rbwinston Guest
|
Posted: Wed Jan 19, 2005 12:32 am Post subject: Re: Object persistence and private fields |
|
|
Read up on TComponent.DefineProperties.
"Dave White" <nospam (AT) spam (DOT) com> wrote:
| Quote: | I have a class that stores project data which is used throughout an
application. I am only accessing the fields using properties via getters
and setters, so the main code can't see the internal fields. However, some
of these properties need to be read-only for security, an example being
"LastTouched" - the app needs to read that to see when the data was last
changed, but I only want the FLastTouched field to be modified within the
class - when another field is modified. This works fine to this point, but
I'm not sure how to handle this in the persistence layer.
The data is stored in a Firebird database, and tranferred to a class
instance via a simple persistence layer located in a datamodule. The module
has a couple of methods, OpenProject(TProject) and SaveProject(TProject). I
pass an instance of the TProject class to the module, which then reads the
data from the database and fill the instance via the properties. The
problem is, it can't update the read only properties, and it can't see the
private fields because the data module and class declerations are in
different units (to allow for different persistence layers down the road).
Have I made a blatant design fault here, or if not, how do I get around this
problem?
For other classes that I've designed to this point, I've put the class
decleration in the same unit as the
data module - but tghis is messy, and exposes the data module to the rest
of the app, which I'd rather not do.
Can anybody point me in the right direction?
Thanks
Dave White
SpectraChrom Software
www.spectrachrom.com
|
|
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Wed Jan 19, 2005 12:39 am Post subject: Re: Object persistence and private fields |
|
|
| Quote: | Read up on TComponent.DefineProperties.
|
Another option is to have protected methods that set values, and promote
the methods in a dummy BO class defined in the implementation section of
your persistence unit, casting the BO to this dummy type.
Cheers,
Jim Cooper
_______________________________________________
Jim Cooper [email]jim (AT) falafelsoft (DOT) com[/email]
Falafel Software http://www.falafelsoft.com
_______________________________________________
|
|
| Back to top |
|
 |
Dave White Guest
|
Posted: Wed Jan 19, 2005 12:39 am Post subject: Re: Object persistence and private fields |
|
|
"rbwinston" <rbwinston (AT) mindspring (DOT) com> wrote
| Quote: | Read up on TComponent.DefineProperties.
Not sure how that applies to my predicament. |
|
|
| Back to top |
|
 |
Dave White Guest
|
Posted: Wed Jan 19, 2005 12:44 am Post subject: Re: Object persistence and private fields |
|
|
"Jim Cooper" <jim (AT) falafelsoft (DOT) com> wrote
| Quote: |
Read up on TComponent.DefineProperties.
Another option is to have protected methods that set values, and promote
the methods in a dummy BO class defined in the implementation section of
your persistence unit, casting the BO to this dummy type.
|
That'll work, and will be easy to implement - thanks Jim.
|
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Wed Jan 19, 2005 1:24 am Post subject: Re: Object persistence and private fields |
|
|
| Quote: | That'll work, and will be easy to implement - thanks Jim.
|
No worries :-)
Cheers,
Jim Cooper
_______________________________________________
Jim Cooper [email]jim (AT) falafelsoft (DOT) com[/email]
Falafel Software http://www.falafelsoft.com
_______________________________________________
|
|
| Back to top |
|
 |
Joanna Carter (TeamB) Guest
|
Posted: Wed Jan 19, 2005 9:07 am Post subject: Re: Object persistence and private fields |
|
|
"rbwinston" <rbwinston (AT) mindspring (DOT) com> a écrit dans le message de news:
[email]4iaru0lavoi596j0t0gge824mjfuva51tu (AT) 4ax (DOT) com[/email]...
| Quote: | Read up on TComponent.DefineProperties.
|
This is only good if you are deriving from TComponent and wanting to stream
to a .dfm file or the like.
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Joanna Carter (TeamB) Guest
|
Posted: Wed Jan 19, 2005 9:54 am Post subject: Re: Object persistence and private fields |
|
|
"Dave White" <nospam (AT) spam (DOT) com> a écrit dans le message de news:
41edad68$1 (AT) newsgroups (DOT) borland.com...
| Quote: | That'll work, and will be easy to implement - thanks Jim.
|
The 'full blown' idea that several people use is a Value Type Framework,
where the business object has a list of Value Type objects as the private
'fields'. Normal code can't access these Value Types but you can use the
protected access trick that Jim suggests to get at them. The advantage of
this system is that it allows you more metadata, even better than RTTI.
If this seems like overkill, then Jims trick on its own may do fine; I use
the enhanced metadata of the Value Types to dynamically create the SQL
required to CRUD the objects.
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Peter Morris [Droopy eyes Guest
|
Posted: Wed Jan 19, 2005 4:56 pm Post subject: Re: Object persistence and private fields |
|
|
What about some kind of a visitor pattern?
The persistence layer sends a SaveToDB visitor to the object, the object
calls back the SaveToDB for each value it has. You could do the same for
fetching.
This wouldn't require you to expose your private properties in any way.
--
Pete
====
ECO Modeler, Audio compression components, DIB graphics controls,
FastStrings
http://www.droopyeyes.com
Read or write articles on just about anything
http://www.HowToDoThings.com
My blog
http://blogs.slcdug.org/petermorris/
|
|
| Back to top |
|
 |
Dave White Guest
|
Posted: Wed Jan 19, 2005 6:30 pm Post subject: Re: Object persistence and private fields |
|
|
"Dave White" <nospam (AT) spam (DOT) com> wrote
Thanks for all the ideas. As I'm looking for something simple and fast, and
this only affects a few fields, I'm going to go with Jim's solution.
Dave White
|
|
| Back to top |
|
 |
Dave White Guest
|
Posted: Thu Jan 20, 2005 5:40 pm Post subject: Re: Object persistence and private fields |
|
|
"siegfriedn" <sniedinger@yahoodotcodotuk> wrote
| Quote: |
Suggestion..
In the Setter procedure only assign a value to FLastTouch when the
component is in the loading fase as in
I had a similar thought yesterday. I'm not streaming components, so I can't |
use ComponentState, however, what I did follows - it may be a bit of a
kludge, but it works and is easy to implement.
In the class, I included a read only property "AllowPrivateWrite", with a
boolean field FAllowPrivateWrite behind this. I set this field to false in
the constructor. Now I make my read-only properties read/write, and in
their setters check the value of FAllowPrivateWrite, if it's true, the value
can be set, otherwise I raise an exception. Before calling the persistence
layer, the field is set to true from within the class, then cleared
afterwards.
This allows the persistence layer to set up the class through the
properties, but if the main app tries to set the "read-only" properties, an
exception will be raised.
Dave White
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|