Monday, March 26, 2012

oop advice needed

On a asp.net project I'm using generated classes to access the
database. I'm developing business logic classes to handle all business
logic and pass on info to the data access classes. On the code behind
pages of the asp.net pages I simply get the values from the postback
and set the properties of the business classes. Before I pass the
values on the the data access layer to persist the changes I'd like to
check if the required properties have been set correctly.

I'm unsure how best to implement these kinds of checks. Should I
simply add a function to the business layer class called something
like:

function isReadyToPersist() as boolean

and do my checks here? Something like:

if me.PersonId = 0 then
return false
end if

etc.

or is there a better way?wapsiii wrote:
> I'm unsure how best to implement these kinds of checks. Should I
> simply add a function to the business layer class called something
> like:
> function isReadyToPersist() as boolean
> and do my checks here? Something like:
> if me.PersonId = 0 then
> return false
> end if

Yes, you could make a check function to make sure the data is correct at the
end of processing, although it might be best to do it in several layers:
1) Use form validation controls to make sure the data is in the correct
format before it gets anywhere near the business objects, and then
2) Make sure the data is correct as it's being entered into the objects,
e.g. if you have a setPersonID function to change the value of an
encapsulated variable called personID then you could put the logic for
testing the input into the setPersonID function and return false if there
was a problem. Then you could easily send an error message back to the
client if any of the set functions failed.
Properties are perfect for this sort of thing. Example:

private string _FirstName;
public string Firstname
{
get { return _FirstName; }
set
{
if (value == null || value.Length > 128)
throw new ArgumentOutOfRangeException("FirstName");
_FirstName = value;
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"wapsiii" <wapsiii@.otmail.com> wrote in message
news:ukink15bfdifje7neu6s18p31q7ej0mk47@.4ax.com...
> On a asp.net project I'm using generated classes to access the
> database. I'm developing business logic classes to handle all business
> logic and pass on info to the data access classes. On the code behind
> pages of the asp.net pages I simply get the values from the postback
> and set the properties of the business classes. Before I pass the
> values on the the data access layer to persist the changes I'd like to
> check if the required properties have been set correctly.
> I'm unsure how best to implement these kinds of checks. Should I
> simply add a function to the business layer class called something
> like:
> function isReadyToPersist() as boolean
> and do my checks here? Something like:
> if me.PersonId = 0 then
> return false
> end if
> etc.
> or is there a better way?
The best way requires a value judgement. But I can give you an example I
have used in numerous projects.
The main idea of this process is that all your objects need to handle, in a
consistent manner, errors detected.
All objects need to respond at a minimum common set of method names. For my
applications it works like this:

First all business objects implement 4 basic methods:
public function getItem(ses as dbConnection, key as String ) as Boolean
public function updateItem(ses as dbConnection) as Boolean
public function deleteItem(ses as dbConnection, key as string ) as Boolean
public function addItem(ses as dbConnection, key as string) as Boolean

The objects then need to implement a propertey which is read only which
returns any error messages detected in object operations.
I use LastOperationStatus as the property name.

Internally in each business object you would define a method to check the
values of the object

Private Function IsValid() as boolean

Each of the , getItem, AddItem, DeleteItem, UpdateItem methods would call
IsValid as the first step in the process

LastOperationStatus would contain the error message of the operation failed.

For example UpdateItem might be implemented as:

Public Function UpdateItem(ses as dbconnection) as boolean.
if not Isvalid then Return False 'isValid is responsible for loading the
error message in LastOperation status
... other code to save data
End function

The object IsValid function may look like this:

Public Function IsValid() as boolean
dim msg as string
if trim(me.FirstName) = "" then
msg = msg & vbcrlf & "The first name cannot be blank"
end if

'you can exit here or build up a list of all the errors if you like.
if msg <> "" then
LastOperationStatus = Msg
return False
else
return true
end if
end function

You also need to validate User input before setting Object Properties...
But how that is done is another subject.

This is intended to outline how I implement the business objects to check
themselves.

"wapsiii" <wapsiii@.otmail.com> wrote in message
news:ukink15bfdifje7neu6s18p31q7ej0mk47@.4ax.com...
> On a asp.net project I'm using generated classes to access the
> database. I'm developing business logic classes to handle all business
> logic and pass on info to the data access classes. On the code behind
> pages of the asp.net pages I simply get the values from the postback
> and set the properties of the business classes. Before I pass the
> values on the the data access layer to persist the changes I'd like to
> check if the required properties have been set correctly.
> I'm unsure how best to implement these kinds of checks. Should I
> simply add a function to the business layer class called something
> like:
> function isReadyToPersist() as boolean
> and do my checks here? Something like:
> if me.PersonId = 0 then
> return false
> end if
> etc.
> or is there a better way?
thanks, I'm slowy getting the hang of oop. I can see that I'm working
along the same lines you outline, but not as consistently as you
outline. Its really a great help to read you comments.

Do you create a an interface with getItem, updateItem, deleteItem and
addItem and have your business classes implement this interface? Or
you you inherit from an abstract class?

Regarding error/exception handling I have two readonly properties
(HasError boolean and ErrorMessage String). I guess HasError is really
not needed! I'm not sure if I should always have the business class
catch errors and only have the calling class check ErrorMessage or if
I should throw an error in the business class and have the asp.net
pages catch the error!

Morten

On Wed, 12 Oct 2005 09:23:08 -0400, "Don Nablo" <nablo@.bellsouth.net>
wrote:

>The best way requires a value judgement. But I can give you an example I
>have used in numerous projects.
>The main idea of this process is that all your objects need to handle, in a
>consistent manner, errors detected.
>All objects need to respond at a minimum common set of method names. For my
>applications it works like this:
>
>First all business objects implement 4 basic methods:
>public function getItem(ses as dbConnection, key as String ) as Boolean
>public function updateItem(ses as dbConnection) as Boolean
>public function deleteItem(ses as dbConnection, key as string ) as Boolean
>public function addItem(ses as dbConnection, key as string) as Boolean
>The objects then need to implement a propertey which is read only which
>returns any error messages detected in object operations.
>I use LastOperationStatus as the property name.
>Internally in each business object you would define a method to check the
>values of the object
>Private Function IsValid() as boolean
>Each of the , getItem, AddItem, DeleteItem, UpdateItem methods would call
>IsValid as the first step in the process
>LastOperationStatus would contain the error message of the operation failed.
>For example UpdateItem might be implemented as:
>Public Function UpdateItem(ses as dbconnection) as boolean.
> if not Isvalid then Return False 'isValid is responsible for loading the
>error message in LastOperation status
> ... other code to save data
>End function
>The object IsValid function may look like this:
>Public Function IsValid() as boolean
> dim msg as string
> if trim(me.FirstName) = "" then
> msg = msg & vbcrlf & "The first name cannot be blank"
> end if
> 'you can exit here or build up a list of all the errors if you like.
> if msg <> "" then
> LastOperationStatus = Msg
> return False
> else
> return true
> end if
>end function
>
>You also need to validate User input before setting Object Properties...
>But how that is done is another subject.
>This is intended to outline how I implement the business objects to check
>themselves.
>
>
>
>"wapsiii" <wapsiii@.otmail.com> wrote in message
>news:ukink15bfdifje7neu6s18p31q7ej0mk47@.4ax.com...
>>
>> On a asp.net project I'm using generated classes to access the
>> database. I'm developing business logic classes to handle all business
>> logic and pass on info to the data access classes. On the code behind
>> pages of the asp.net pages I simply get the values from the postback
>> and set the properties of the business classes. Before I pass the
>> values on the the data access layer to persist the changes I'd like to
>> check if the required properties have been set correctly.
>>
>> I'm unsure how best to implement these kinds of checks. Should I
>> simply add a function to the business layer class called something
>> like:
>>
>> function isReadyToPersist() as boolean
>>
>> and do my checks here? Something like:
>>
>> if me.PersonId = 0 then
>> return false
>> end if
>>
>> etc.
>>
>> or is there a better way?
I wonder why you have a key param in you public function addItem? What
key do you pass to this function?

On Wed, 12 Oct 2005 09:23:08 -0400, "Don Nablo" <nablo@.bellsouth.net>
wrote:

>The best way requires a value judgement. But I can give you an example I
>have used in numerous projects.
>The main idea of this process is that all your objects need to handle, in a
>consistent manner, errors detected.
>All objects need to respond at a minimum common set of method names. For my
>applications it works like this:
>
>First all business objects implement 4 basic methods:
>public function getItem(ses as dbConnection, key as String ) as Boolean
>public function updateItem(ses as dbConnection) as Boolean
>public function deleteItem(ses as dbConnection, key as string ) as Boolean
>public function addItem(ses as dbConnection, key as string) as Boolean
>The objects then need to implement a propertey which is read only which
>returns any error messages detected in object operations.
>I use LastOperationStatus as the property name.
>Internally in each business object you would define a method to check the
>values of the object
>Private Function IsValid() as boolean
>Each of the , getItem, AddItem, DeleteItem, UpdateItem methods would call
>IsValid as the first step in the process
>LastOperationStatus would contain the error message of the operation failed.
>For example UpdateItem might be implemented as:
>Public Function UpdateItem(ses as dbconnection) as boolean.
> if not Isvalid then Return False 'isValid is responsible for loading the
>error message in LastOperation status
> ... other code to save data
>End function
>The object IsValid function may look like this:
>Public Function IsValid() as boolean
> dim msg as string
> if trim(me.FirstName) = "" then
> msg = msg & vbcrlf & "The first name cannot be blank"
> end if
> 'you can exit here or build up a list of all the errors if you like.
> if msg <> "" then
> LastOperationStatus = Msg
> return False
> else
> return true
> end if
>end function
>
>You also need to validate User input before setting Object Properties...
>But how that is done is another subject.
>This is intended to outline how I implement the business objects to check
>themselves.
>
>
>
>"wapsiii" <wapsiii@.otmail.com> wrote in message
>news:ukink15bfdifje7neu6s18p31q7ej0mk47@.4ax.com...
>>
>> On a asp.net project I'm using generated classes to access the
>> database. I'm developing business logic classes to handle all business
>> logic and pass on info to the data access classes. On the code behind
>> pages of the asp.net pages I simply get the values from the postback
>> and set the properties of the business classes. Before I pass the
>> values on the the data access layer to persist the changes I'd like to
>> check if the required properties have been set correctly.
>>
>> I'm unsure how best to implement these kinds of checks. Should I
>> simply add a function to the business layer class called something
>> like:
>>
>> function isReadyToPersist() as boolean
>>
>> and do my checks here? Something like:
>>
>> if me.PersonId = 0 then
>> return false
>> end if
>>
>> etc.
>>
>> or is there a better way?

0 comments:

Post a Comment