| View previous topic :: View next topic |
| Author |
Message |
darkattraction Guest
|
Posted: Thu Oct 16, 2003 7:56 pm Post subject: Null Values |
|
|
I'm a newbie and I read a lot about Business Object (amber, Joanna
Carter... )
.... and I wonder how can I handle null values if I write for example,
TEmployee=class(TPersistentObject)
published
...
property Birthdate: TDateTime...
...
end;
Have I to define a class like TDateTimeProperty as to handle null
value or dictate 0 is null - but how to with integers and doubles?
Mark
|
|
| Back to top |
|
 |
Joanna Carter Guest
|
Posted: Thu Oct 16, 2003 10:19 pm Post subject: Re: Null Values |
|
|
| Have I to define a class like TDateTimeProperty as to handle null
| Quote: | value or dictate 0 is null - but how to with integers and doubles?
|
I use an Attribute framework for all value types and that has the concept of
a null value for any type in the system.
Joanna
--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Franz-Leo Chomse Guest
|
Posted: Fri Oct 17, 2003 9:00 am Post subject: Re: Null Values |
|
|
| Quote: | Have I to define a class like TDateTimeProperty as to handle null
value or dictate 0 is null - but how to with integers and doubles?
|
Since most data types have no specific value for NULL you have
to use an additional flag. In case of database fields these flags are
part of the record buffer for properties you either have to create
an attribute framework or need some kind of reflection support which
is not available for the normal Delphi and Kylix, but part of the .NET
CLR
Regards from Germany
Franz-Leo
|
|
| Back to top |
|
 |
darkattraction Guest
|
Posted: Sat Oct 18, 2003 2:17 pm Post subject: Re: Null Values |
|
|
I understand I have to use some kind of attribute/property framework
but I don't see in which way reflection can be useful.
Could you explain it?
Mark
"Franz-Leo Chomse" <franz-leo.chomse (AT) samac (DOT) de> wrote
| Quote: |
Have I to define a class like TDateTimeProperty as to handle null
value or dictate 0 is null - but how to with integers and doubles?
Since most data types have no specific value for NULL you have
to use an additional flag. In case of database fields these flags are
part of the record buffer for properties you either have to create
an attribute framework or need some kind of reflection support which
is not available for the normal Delphi and Kylix, but part of the .NET
CLR
Regards from Germany
Franz-Leo
|
|
|
| Back to top |
|
 |
Franz-Leo Chomse Guest
|
Posted: Mon Oct 20, 2003 9:07 am Post subject: Re: Null Values |
|
|
On Sat, 18 Oct 2003 16:17:02 +0200, "darkattraction"
<darkattraction (AT) free (DOT) fr> wrote:
| Quote: | I understand I have to use some kind of attribute/property framework
but I don't see in which way reflection can be useful.
Could you explain it?
|
A powerfull reflection framework contains the attribute framework as
it allows to support metadata. So if there isn't already a metadata
for NULL you can add a user defined attribute for it.
Regards from Germany
Franz-Leo
|
|
| Back to top |
|
 |
Joanna Carter Guest
|
Posted: Mon Oct 20, 2003 12:57 pm Post subject: Re: Null Values |
|
|
Franz-Leo Chomse wrote:
| Quote: | A powerfull reflection framework contains the attribute framework as
it allows to support metadata. So if there isn't already a metadata
for NULL you can add a user defined attribute for it.
|
Just a quick example:
TAttribute = class
....
protected
function IsNull: Boolean; virtual; abstract;
procedure SetNull; virtual; abstract;
end;
TDateAttribute = class(TAttribute)
private
fValue: TDateTime;
....
public
function IsNull: Boolean; override;
procedure SetNull; override;
end;
TIntegerAttribute = class(TAttribute)
private
fIsNull: Boolean;
fValue: Integer;
....
public
constructor Create;
...
procedure SetValue(Value: Integer);
function IsNull: Boolean; override;
procedure SetNull; override;
end;
implementation
const
NullDateTime = -693594;
function TDateAttribute.IsNull: Boolean;
begin
Result := fValue = NullDateTime;
end;
procedure TDateAttribute.SetNull;
begin
fValue := NullDateTime;
end;
constructor TIntegerAttribute.Create;
begin
inherited Create;
SetNull;
end;
procedure TIntegerAttribute.SetValue(Value: Integer);
begin
fValue := Value;
fIsNull := False;
end;
function TIntegerAttribute.IsNull: Boolean;
begin
Result := fIsNull;
end;
procedure TIntegerAttribute.SetNull;
begin
fValue := 0;
fIsNull := True;
end;
Joanna
--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Ritchie Annand Guest
|
Posted: Tue Oct 21, 2003 9:40 am Post subject: Re: Null Values |
|
|
In article <3f93dc5a$1 (AT) newsgroups (DOT) borland.com>, [email]joannac (AT) btinternet (DOT) com[/email]
says...
| Quote: | Franz-Leo Chomse wrote:
| A powerfull reflection framework contains the attribute framework as
| it allows to support metadata. So if there isn't already a metadata
| for NULL you can add a user defined attribute for it.
Just a quick example:
TAttribute = class
...
protected
function IsNull: Boolean; virtual; abstract;
procedure SetNull; virtual; abstract;
end;
|
They apparently were concerned about this in .NET, too, to read the docs.
They made an entire separate tree for values in SQL, the only difference
being that the SQL/DB-related ones had an extra property for null.
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemdatasqltypes.asp
They all implement INullable, too. Whee :)
-- Ritchie Annand
Senior Software Architect
Malibu Software & Engineering Ltd.
Business: http://www.malibugroup.com
Personal: http://nimble.nimblebrain.net
Wiki: http://wiki.nimblebrain.net
|
|
| Back to top |
|
 |
Aivar Annamaa Guest
|
Posted: Wed Oct 29, 2003 8:36 am Post subject: Re: Null Values |
|
|
Joanna Carter wrote:
| Quote: | TIntegerAttribute = class(TAttribute)
private
fIsNull: Boolean;
fValue: Integer;
....
procedure SetValue(Value: Integer);
|
If I understand correctly, then declaration of a domain class would be
similar to this:
TPerson = class
published
property Weight:TIntegerAttribute read getWeight write setWeight;
....
end;
But that way TPerson class can't really control changing the value of
Weight in SetWeight, because client can use
person.Weight.setValue(whaterver);
For this reason it seems to me very cumbersome to use objects for
representing primitive values.
What about using Variants instead?
One could define several "variant types" like this
TIntegerVariant = Variant;
TStringVariant = Variant;
....
to allow showing info about the real attribute type in class declaration.
Aivar
|
|
| Back to top |
|
 |
Joanna Carter Guest
|
Posted: Wed Oct 29, 2003 9:12 am Post subject: Re: Null Values |
|
|
Aivar Annamaa wrote:
| Quote: | If I understand correctly, then declaration of a domain class would be
similar to this:
TPerson = class
published
property Weight:TIntegerAttribute read getWeight write setWeight;
...
end;
|
Not quite...
TAttributeList = class
procedure Add(const Attrib: TAttribute);
....
end;
TObjectAttribute = class(TAttribute)
private
fAttributes: TAttributeList;
protected
function GetAttributes: TAttributeList;
....
end;
TPerson = class(TObjectAttribute)
public
constructor Create;
property Weight:Integer
read GetWeight
write SetWeight;
....
end;
constructor TPerson.Create;
begin
inherited Create;
GetAttributes.Add(TIntegerAttribute.Create('Weight'));
...
end;
function GetWeight: Integer;
begin
Result := TIntegerAttribute(GetAttributes['Weight']).GetValue;
end;
procedure SetWeight(Value: Integer);
begin
TIntegerAttribute(GetAttributes['Weight']).SetValue(Value);
end;
Joanna
--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Solerman Kaplon Guest
|
Posted: Wed Oct 29, 2003 11:07 am Post subject: Re: Null Values |
|
|
Hi,
There are many possible variations of knowing when an attibute changes,
would be a good thing to use the observer pattern on the attribute to
know when it changes? possible allowing some kind of
constraint/validation manager connect to it and provide ways of
validating data. Is this the right place to do it? I mean, the data may
get an invalid status, and the presentation layer can observe the
validation manager to determine what to do. The validation can take
place before the data gets assigned to the internal fields, providing a
just-in-time validation when appopriate. Do you see any performance
problems in this approach? just wondering... :)
Solerman
Joanna Carter wrote:
| Quote: | Not quite...
TAttributeList = class
procedure Add(const Attrib: TAttribute);
...
end;
TObjectAttribute = class(TAttribute)
private
fAttributes: TAttributeList;
protected
function GetAttributes: TAttributeList;
...
end;
TPerson = class(TObjectAttribute)
public
constructor Create;
property Weight:Integer
read GetWeight
write SetWeight;
...
end;
constructor TPerson.Create;
begin
inherited Create;
GetAttributes.Add(TIntegerAttribute.Create('Weight'));
...
end;
function GetWeight: Integer;
begin
Result := TIntegerAttribute(GetAttributes['Weight']).GetValue;
end;
procedure SetWeight(Value: Integer);
begin
TIntegerAttribute(GetAttributes['Weight']).SetValue(Value);
end;
|
|
|
| Back to top |
|
 |
Aivar Annamaa Guest
|
Posted: Wed Oct 29, 2003 11:21 am Post subject: Re: Null Values |
|
|
Thanks Joanna!
So, you are using a collection of attributes in your BO-s and provide
direct links to the items of that collection via properties, right?
And such way one doesn't have to mess with RTTI but can use
GetAttributes to inspect a BO of unknown type (when persisting or
displaying an object), yes?
But still, about assigning null to a person's weight, it seems that it's
possible only via attributes collection, not via respective property.
And if client wants to read "weight" it has to check from the attribute
collection if the weight property represents currently real weight or
the actual value is not known? If so, then whats the use of this integer
property at all? (besides that it communicates the structure of the object)
Aivar
|
|
| Back to top |
|
 |
Joanna Carter Guest
|
Posted: Wed Oct 29, 2003 12:00 pm Post subject: Re: Null Values |
|
|
Solerman Kaplon wrote:
| Quote: | There are many possible variations of knowing when an attibute
changes, would be a good thing to use the observer pattern on the
attribute to know when it changes? possible allowing some kind of
constraint/validation manager connect to it and provide ways of
validating data. Is this the right place to do it? I mean, the data
may get an invalid status, and the presentation layer can observe the
validation manager to determine what to do. The validation can take
place before the data gets assigned to the internal fields, providing
a just-in-time validation when appopriate. Do you see any performance
problems in this approach? just wondering...
|
You seem to have got the right idea. Performance is only going to be a
problem in batch update situations, but time between single updates usually
measured in seconds due to user inefficiency rather than in milliseconds due
to extra code.
Joanna
--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Joanna Carter Guest
|
Posted: Wed Oct 29, 2003 12:07 pm Post subject: Re: Null Values |
|
|
Aivar Annamaa wrote:
| Quote: | So, you are using a collection of attributes in your BO-s and provide
direct links to the items of that collection via properties, right?
|
Right
| Quote: | And such way one doesn't have to mess with RTTI but can use
GetAttributes to inspect a BO of unknown type (when persisting or
displaying an object), yes?
|
Absolutely
| Quote: | But still, about assigning null to a person's weight, it seems that
it's possible only via attributes collection, not via respective
property.
|
Assigning null to an attribute is not going to be something that a user can
do direcly. It is usually something that would be done in response to a user
clearing an edit. If you use a GUI framework like MVP, then that is
something that the Interactopr wouold detect and then use the metadata
provided by the attribute framework to call the attribute's SetNull method.
| Quote: | And if client wants to read "weight" it has to check from the
attribute collection if the weight property represents currently real
weight or
the actual value is not known? If so, then whats the use of this
integer property at all? (besides that it communicates the structure
of the object)
|
The only clients that are really going to have to do any talking to the
IsNull state of an attribute are the OPF and GUI and both of these. Talking
to the properties of a BO when the state of the underlying attribute is null
would usually yield the default nil/zero value.
Unless you can think of a better way of doing it??
Joanna
--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Aivar Annamaa Guest
|
Posted: Wed Oct 29, 2003 1:01 pm Post subject: Re: Null Values |
|
|
Joanna Carter wrote:
| Quote: | Unless you can think of a better way of doing it??
|
Unfourtunately not :)
If attributes were variants, then one could use same property both for
setting/getting nulls and other values. PHP is my main language, so I'm
used with loose types, but in Delphi I don't feel right when using
Variants. So, I'm not happy with using variant attributes and I will
consider using a collection of attributes.
Thanks for help!
Aivar
|
|
| Back to top |
|
 |
|