So, yesterday I was implementing a new BusinessBase<T> derived class.
Through an oversight on my part, here is what happened.
I already had: (substituting class names for discussion)
class FirstClass : BusinessBase<FirstClass> ...
Then I went to implement SecondClass, and incorrectly declared it...
class SecondClass : BusinessBase<FirstClass> ...
This seemed to satisfy the generic constraint requirements and was not caught until runtime when I tried to Save the object and got an invalid cast exception because you can't cast a FirstClass to a SecondClass.
I actually have a common MyBusinessBase<T> class in between my classes and BusinessBase<T>. Is there something I could put in the declaration or implementation that would catch errors like this?
It isn't always an error. In fact I have an inheritance hierarchy setup that way.
The "problem" is that FirstClass is probably fetching and saving items as FirstClass.
In my hierarchy, since I know I will be deriving SecondClass from FirstClass, the FirstClass code fetches and saves items as type SecondClass.
e.g.
FirstClass=MyAccountGen
SecondClass=MyAccount
All the code generated stuff in MyAccountGen like Fetch and Save refer to MyAccount.
Joe
I see. But is your declaration:
class SecondClass : FirstClass
or
class SecondClass : BusinessBase<FirstClass>
The first declaration would seem to define an "is-a" relationship (e.g. a SecondClass is-a FirstClass), while I don't think this true of the latter case.
class SecondClass : FirstClass
Copyright (c) Marimer LLC