PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Class.New and DB mapping

Reply

Class.New and DB mapping

 
Thread Tools Rate Thread
Old 16-11-2006, 08:44 AM   #1
myname
Guest
 
Posts: n/a
Default Class.New and DB mapping


Hello,

I map objects to a DB.

Currently, I have a shared function like :
clsCustomer.getForId(customerId as integer) as clsCustomer.
It returns Nothing when the customerId does not exist.

I would like to do that within the New :
clsCustomer.New(customerId as integer)

But how can I handle the non-existence of customerId then ?

Is it possible that New returns Nothing ?

Would it be better to throw an Exception ?

Thanks !


  Reply With Quote
Old 16-11-2006, 01:17 PM   #2
Robinson
Guest
 
Posts: n/a
Default Re: Class.New and DB mapping


I wouldn't advise you to do anything hardcore like that within the
constructor. Logically, you should throw an exception, because constructing
the object with the required state has failed. You may also consider
setting an "IsValid" flag inside the class set to true or false depending on
constructor success or failure, however that is a slippery slope. My
preferred solution would be:


Dim theClass As New myClass

Result = theClass.LoadCustomer ( theID )



or in the case of your instance factory:



Dim theClass As myClass = clsCustomer.getForId ( customerId )

If theClass Is Nothing Then
' Failed.
End If


There is a lot to be said for being more explicit in code and less
obfuscating .





"myname" <nospam@ihatespam.com> wrote in message
news:455c21be$1@news.sncf.fr...
> Hello,
>
> I map objects to a DB.
>
> Currently, I have a shared function like :
> clsCustomer.getForId(customerId as integer) as clsCustomer.
> It returns Nothing when the customerId does not exist.
>
> I would like to do that within the New :
> clsCustomer.New(customerId as integer)
>
> But how can I handle the non-existence of customerId then ?
>
> Is it possible that New returns Nothing ?
>
> Would it be better to throw an Exception ?
>
> Thanks !
>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off