Class.New and DB mapping

  • Thread starter Thread starter myname
  • Start date Start date
M

myname

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 !
 
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 ;).
 
Back
Top