J
Jerad Rose
I'm relatively new to C# and polymorphism, so what I'm trying to accomplish
may not be possible, or there may be a totally different approach that I
should be taking.
I have a base class (MyBase) and a collection of MyBase objects
(MyBaseCollection). Now, I also have an extension to MyBase (MyExtendedBase
: MyBase) that has additional properties. And lastly, I have a collection
of MyExtendedBase objects(MyExtendedBaseCollection : MyBaseCollection).
I have a data layer that has a method (GetMyBaseCollection) that retrieves
records from the database, and populates a MyBaseCollection with these
records. However, there are cases where I need to use this method to
populate a MyExtendedBaseCollection. But here's the catch -- I want my data
layer to be unaware (and independent) of MyExtendedBase and
MyExtendedBaseCollection.
I have tried several different ways to accomplish this, but no matter what I
do, I end up needing to up-cast MyBase to MyExtendedBase, which is illegal
in C#. I need my data layer to be able to accept and populate a
MyBaseCollection (with it possibly being a sub class of a
MyExtendedBaseCollection) with MyBase objects (with them possibly being sub
classes of MyExtendedBase), and then when the collection is returned, the
items in the collection are of the appropriate type (depending on the type
of collection passed in).
I have read about different design patterns (namely Decorator and
Composite), but am not sure if they apply to my situation, and if so, how to
implement them accordingly.
Again, I may be doing something totally illogical, so I'm open to any
suggestions you may have. Also, if I have not provided enough info, let me
know and I will follow up.
Thanks in advance.
Jerad
may not be possible, or there may be a totally different approach that I
should be taking.
I have a base class (MyBase) and a collection of MyBase objects
(MyBaseCollection). Now, I also have an extension to MyBase (MyExtendedBase
: MyBase) that has additional properties. And lastly, I have a collection
of MyExtendedBase objects(MyExtendedBaseCollection : MyBaseCollection).
I have a data layer that has a method (GetMyBaseCollection) that retrieves
records from the database, and populates a MyBaseCollection with these
records. However, there are cases where I need to use this method to
populate a MyExtendedBaseCollection. But here's the catch -- I want my data
layer to be unaware (and independent) of MyExtendedBase and
MyExtendedBaseCollection.
I have tried several different ways to accomplish this, but no matter what I
do, I end up needing to up-cast MyBase to MyExtendedBase, which is illegal
in C#. I need my data layer to be able to accept and populate a
MyBaseCollection (with it possibly being a sub class of a
MyExtendedBaseCollection) with MyBase objects (with them possibly being sub
classes of MyExtendedBase), and then when the collection is returned, the
items in the collection are of the appropriate type (depending on the type
of collection passed in).
I have read about different design patterns (namely Decorator and
Composite), but am not sure if they apply to my situation, and if so, how to
implement them accordingly.
Again, I may be doing something totally illogical, so I'm open to any
suggestions you may have. Also, if I have not provided enough info, let me
know and I will follow up.
Thanks in advance.
Jerad