 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Goran Smith Guest
|
Posted: Thu Apr 06, 2006 3:03 am Post subject: Validation in Business Objects |
|
|
Ho do you handle validation in BO, both at the property/field level
and at object level?
For field level I see 2 possible approaches which must be implemented
by the setter method:
- set a flag
- raise an exception
For object level: a function in the base class, overridden
by all decendents (Validate) which should be called every time
object validation must be performed. This function can return
false (signaling an error) or raise an exception.
What do you think? |
|
| Back to top |
|
 |
sanmaotuo_33@yahoo.com Guest
|
Posted: Thu Apr 06, 2006 4:04 am Post subject: Re: Validation in Business Objects |
|
|
IMO: object level Using Visitor Patterm
1、Each BO Defined as Visited (IVisited)
2、Each BO's Operation(Include Validation) Defined as Visitor
(IVisitor)
For Example:
IVisited = interface
function Accept(const Visitor: IVisitor): Boolean;
end;
IVisitor = interface
function Visit(const Visited: IInterface): Boolean;
end;
IBO = interface(IVisited)
function GetOID: string;
procedure SetOID(const Value: string);
property OID: string read GetOID write SetOID;
......
end;
TBO = class(TInterfacedObject, IBO)
private
FOID: string;
......
protected
//IBO
function GetOID: string;
procedure SetOID(const Value: string);
......
//IVisited
function Accept(const Visitor: IInterface): Boolean;
public
......
end;
function TBO.Accept(const Visitor: IInterface): Boolean;
begin
Result := Visitor.Visit(Self);
end;
.......
TBOValidationVisitor = class(TInterfacedObject, IVisitor)
private
.......
protected
//IVisitor
function Visit(const Visited: IInterface): Boolean;
public
......
end;
function TBOValidationVisitor.Visit(const Visited: IInterface):
Boolean;
var
Obj: IBO;
begin
Visited.QueryInterface(IBO, Obj);
if Obj <> nil then
begin
//Validation BO
.....
Result := True( or False);
end;
end;
if You want to Validate BO You Just do it Like This:
Result := Accept(TBOValidationVisitor.Create);
Wish My Example Can Help You  |
|
| Back to top |
|
 |
Andy Gibson Guest
|
Posted: Thu Apr 06, 2006 5:03 am Post subject: Re: Validation in Business Objects |
|
|
Goran Smith wrote:
| Quote: | Ho do you handle validation in BO, both at the property/field level
and at object level?
For field level I see 2 possible approaches which must be implemented
by the setter method:
- set a flag
- raise an exception
For object level: a function in the base class, overridden
by all decendents (Validate) which should be called every time
object validation must be performed. This function can return
false (signaling an error) or raise an exception.
What do you think?
|
I'd always use caution when raising errors in the setter method since it
may trigger errors in mid update.
i.e.
Object.BeginDate := Date + 12;
Object.EndDate := Date + 10;
Object.BeginDate := Date;
In the above code, everything should execute ok and be valid, but with
validation on the setter, setting the end date results in a 'begin date
must be before the end date' error.
What you might want is :
OBject.BeginUpdate
try
Object.EndDate := Date+10;
Object.BeginDate := Date;
finally
Object.EndUpdate;
end;
Between the begin and end update statements, validation on setters is
suspended. When EndUpdate is called, it calls the validation routine and
raises any errors.
Another problem with validation on the setters is that it could end up
raising an exception when simply loading an object.
Reading your Post again, I guess it depends on what you mean by field
level. Are we talking about simple validation such as not entering
'sdfsdf' as a date?
I might consider putting the validation into one routine as opposed to
littering it through the object setters and getters. That way it is more
organized. Plus you can call the validation routine independently and
then determine whether to raise an exception or not, or whether you
display the errors to the users another way. Another option is to have
different error types, one level for a data entry error (please enter a
name), another for errors in the application (i.e. Another object XYZ
doesn't exist for this object and it needs one). That way you can
request only lightweight data entry errors and then later on, call the
proc to get any errors, such those that need to call the DB to see if
anything is wrong.
This is pure speculation on my part, it's an issue I have been mulling
over, and thought I'd share my thoughts.
Cheers,
Andy |
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Thu Apr 06, 2006 7:03 am Post subject: Re: Validation in Business Objects |
|
|
| Quote: | Ho do you handle validation in BO, both at the property/field level
and at object level?
|
There's different sorts of validation. One place to do it is in property
setters, one is in a validation routine - which place you do it depends on what
you are trying to do. In the latter case you tend to want to do 2 sorts of
validation - before save and before delete.
Almost always exceptions beat error flags.
Cheers,
Jim Cooper
_____________________________________________
Jim Cooper jcooper (AT) tabdee (DOT) ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk
TurboSync - Connecting Delphi to your Palm
_____________________________________________ |
|
| Back to top |
|
 |
Peter Morris [Droopy eyes Guest
|
Posted: Thu Apr 06, 2006 8:03 am Post subject: Re: Validation in Business Objects |
|
|
I would only throw an exception in a setter if an attempt was made to modify
a value that should be read only.
For everything else I would use a validation routine that returns a list of
errors. I usually display this error list at the bottom of the form
(updates automatically as the error lists changes), I disable the Save
button if there are any errors. As a finaly precaution the class will throw
an exception if you try to save it when it has errors.
Constraint: StartDate <= FinishDate
StartDate := Today - 365
FinishDate := StartDate + 1;
Oops, set the year wrong in my DateTimePicker!
StartDate := Today;
In the first implementation you would get an exception "StartDate cannot be
after FinishDate" and will then have to change the FinishDate first (imagine
having to code checks like this when changing dates in code!), the second
implementation will let you change anything you like in whatever order you
choose, but only throw an exception at the point you try to save.
--
Pete
====
Audio compression components, DIB graphics controls, ECO extensions,
FastStrings
http://www.droopyeyes.com
My blog
http://blogs.slcdug.org/petermorris/ |
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Thu Apr 06, 2006 12:04 pm Post subject: Re: Validation in Business Objects |
|
|
| Quote: | I would only throw an exception in a setter if an attempt was made to modify
a value that should be read only.
|
Would you not just make the property read only?
Cheers,
Jim Cooper
_____________________________________________
Jim Cooper jcooper (AT) tabdee (DOT) ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk
TurboSync - Connecting Delphi to your Palm
_____________________________________________ |
|
| Back to top |
|
 |
Hechicero Guest
|
Posted: Thu Apr 06, 2006 5:03 pm Post subject: Re: Validation in Business Objects |
|
|
Hi,
From my point of view you can use different types of validations:
a) in the setter method if that property isnt related to any other
propertys (to avoid order conflicts) and the validation is simple and quite
obvious (for instance an integer property shouldnt be 45 for some reason)
b) in a method of the object (ej validate). Maybe a BOBase could contain
a virtual validate method.
c) Be aware thare can be different types of validations. the Ones usually
that ocurr during a process, the ones that you have to do before the object
is persisted to de DB, etc
But maybe there is a more complex case
e) at the end of a transaction where many objects participate. this is
more complex and sometimes a validation method is not enough. Maybe you
coud have a mediator perform the validations or a rulesEngine that takes the
collection of objects as an imput and validate them using a collection of
rules. (useful if rules changes frecuently and you dont want to recompile
each time a rule changes)
If you use an OPF The dupplication of validations in objects and database
constraints is something to bear in mind. (manintenance between DB
constrains and objects validations)
Esteban Calabria |
|
| Back to top |
|
 |
Peter Morris [Droopy eyes Guest
|
Posted: Thu Apr 06, 2006 8:03 pm Post subject: Re: Validation in Business Objects |
|
|
| Quote: | Would you not just make the property read only?
|
If it was always read-only then yes, but not if it was read-only based on
its current state no :-)
eg, PurchaseOrder.State == OrderState.Delivered - I shouldn't go changing
the delivery address. |
|
| Back to top |
|
 |
Joanna Carter [TeamB] Guest
|
Posted: Fri Apr 07, 2006 8:03 am Post subject: Re: Validation in Business Objects |
|
|
"Goran Smith" <goran (AT) forgetit (DOT) com> a crit dans le message de news:
g9t832tbn3vlt549ehmq4a39kq2bvvub9b (AT) 4ax (DOT) com...
| For field level I see 2 possible approaches which must be implemented
| by the setter method:
| - set a flag
| - raise an exception
Neither :-)
I have used the idea of passing a null error argument object to the
validation routine, by reference, and then, if the object comes back not
null, I add the object to a list of errors in the business object. This list
can then be inspected at either storage time for objects populated in code
or when trying to exit a control for objects populated in a form.
| For object level: a function in the base class, overridden
| by all decendents (Validate) which should be called every time
| object validation must be performed. This function can return
| false (signaling an error) or raise an exception.
IMO, the only time that object validation is required is just prior to
storage. The above system allows for this.
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer |
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Fri Apr 07, 2006 11:03 am Post subject: Re: Validation in Business Objects |
|
|
| Quote: | eg, PurchaseOrder.State == OrderState.Delivered - I shouldn't go changing
the delivery address.
|
Ah, I see what you mean.
Cheers,
Jim Cooper
_____________________________________________
Jim Cooper jcooper (AT) tabdee (DOT) ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk
TurboSync - Connecting Delphi to your Palm
_____________________________________________ |
|
| Back to top |
|
 |
Peter Morris [Droopy eyes Guest
|
Posted: Fri Apr 07, 2006 12:04 pm Post subject: Re: Validation in Business Objects |
|
|
| Quote: | Ah, I see what you mean.
|
Invalid operation Vs invalid value. I often rush my answers and do not
provide enough information, after all, I know what I mean  |
|
| Back to top |
|
 |
Jim Cooper Guest
|
Posted: Fri Apr 07, 2006 3:03 pm Post subject: Re: Validation in Business Objects |
|
|
| Quote: | Invalid operation Vs invalid value. I often rush my answers and do not
provide enough information, after all, I know what I mean
|
That's OK, I got what you were driving at :-)
Cheers,
Jim Cooper
_____________________________________________
Jim Cooper jcooper (AT) tabdee (DOT) ltd.uk
Skype : jim.cooper
Tabdee Ltd http://www.tabdee.ltd.uk
TurboSync - Connecting Delphi to your Palm
_____________________________________________ |
|
| Back to top |
|
 |
Goran Smith Guest
|
Posted: Wed Apr 12, 2006 3:03 am Post subject: Re: Validation in Business Objects |
|
|
| Quote: | Almost always exceptions beat error flags.
|
Could you explain why? |
|
| Back to top |
|
 |
Goran Smith Guest
|
Posted: Wed Apr 12, 2006 3:03 am Post subject: Re: Validation in Business Objects |
|
|
Andy Gibson <delphi_pa (AT) hotmail (DOT) com> wrote:
| Quote: | I might consider putting the validation into one routine as opposed to
littering it through the object setters and getters. That way it is more
organized. Plus you can call the validation routine independently and
then determine whether to raise an exception or not, or whether you
display the errors to the users another way. Another option is to have
different error types, one level for a data entry error (please enter a
name), another for errors in the application (i.e. Another object XYZ
doesn't exist for this object and it needs one). That way you can
request only lightweight data entry errors and then later on, call the
proc to get any errors, such those that need to call the DB to see if
anything is wrong.
|
Interesting, Thanks. |
|
| Back to top |
|
 |
Goran Smith Guest
|
Posted: Wed Apr 12, 2006 3:03 am Post subject: Re: Validation in Business Objects |
|
|
| Quote: | | For object level: a function in the base class, overridden
| by all decendents (Validate) which should be called every time
| object validation must be performed. This function can return
| false (signaling an error) or raise an exception.
IMO, the only time that object validation is required is just prior to
storage.
|
Why? I expect object validation to be performed in UI when the user
try to edit another object, before storage, for example when he/she
goes from one row to another in a grid. |
|
| 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
|
|