How to force downcast of Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a base class that has been filled with values (for example an Order)

So, something like this

dim ord as new Order(

ord.FirstName = "Clayton
ord.LastName = "Gulick

etc..

Now, I have a derived class called InternalOrder, that extends Order

How can I force the above order into the InternalOrder class

I know, the first question is "Why? Thats bad design!

There is a method to my madness, namely that I want to expose the Order class to customers via a web service. I want this class to be trim and simple, and I don't want the guts of our internal variables to show to the world

However, when an Order is submitted from the Web Service, I need to stick it into InternalOrder so I can do some additional processing on it and fill additional internal values (for example, integrate with legacy system etc...

It is painful to have to write all the code to manually copy all the properties down to the InternalOrder class

The only thing I can think to do is to use reflection and enumerate through all the properties and copy all of the public ones over. This gets sticky though because I would have to recursively navigate the Order to also copy all of the sub-object properties (Address, BillingInfo etc...)

Sure would be nice if I could do something like

dim ord as new Order(
dim IntOrd as new InternalOrder(

IntOrd = ForceCast(ord, InternalOrder

If not, I'll go ahead and write a generic static ForceCast class and post the code, but I am hoping there is a better way.
 
I faced a similar situation recently. I see two possibilities.

Your solution is one of them. Except, I would prefer to code like this:

Dim ord as new Order()
Dim IntOrd as InternalOrder() = ord.ConvertToInternalOrder()


The other alternative is like this:

Dim ord as New Order()
Dim IntOrd as InternalOrder = New InternalOrder(ord)

where you define constructors like:

Class InternalOrder
Inherits Order
Public Sub New(order As Order)
MyBase.New()
...
End Sub

Class Order
Protected Sub New()
...


I like the neatness of the second alternative, but it could involve more
processing. For my situation, it would have meant an extra database access,
so I chose the first alternative.

Hope this helps.


Clayton Gulick said:
I have a base class that has been filled with values (for example an Order).

So, something like this:

dim ord as new Order()

ord.FirstName = "Clayton"
ord.LastName = "Gulick"

etc...

Now, I have a derived class called InternalOrder, that extends Order.

How can I force the above order into the InternalOrder class?

I know, the first question is "Why? Thats bad design!"

There is a method to my madness, namely that I want to expose the Order
class to customers via a web service. I want this class to be trim and
simple, and I don't want the guts of our internal variables to show to the
world.
However, when an Order is submitted from the Web Service, I need to stick
it into InternalOrder so I can do some additional processing on it and fill
additional internal values (for example, integrate with legacy system
etc...)
It is painful to have to write all the code to manually copy all the
properties down to the InternalOrder class.
The only thing I can think to do is to use reflection and enumerate
through all the properties and copy all of the public ones over. This gets
sticky though because I would have to recursively navigate the Order to also
copy all of the sub-object properties (Address, BillingInfo etc...).
Sure would be nice if I could do something like:

dim ord as new Order()
dim IntOrd as new InternalOrder()

IntOrd = ForceCast(ord, InternalOrder)

If not, I'll go ahead and write a generic static ForceCast class and post
the code, but I am hoping there is a better way.
 
Clayton Gulick said:
I have a base class that has been filled with values (for example an Order).

So, something like this:

dim ord as new Order()

ord.FirstName = "Clayton"
ord.LastName = "Gulick"

etc...

Now, I have a derived class called InternalOrder, that extends Order.

How can I force the above order into the InternalOrder class?

You can't. You can't change the actual type of an object, nor treat an
instance of a base class as an instance of the derived class.

What you *could* do, however, is have a constructor to InternalOrder
which takes an existing Order as a parameter and copies data from that
order into the new InternalOrder.
I know, the first question is "Why? Thats bad design!"

There is a method to my madness, namely that I want to expose the
Order class to customers via a web service. I want this class to be
trim and simple, and I don't want the guts of our internal variables
to show to the world.

So use a factory to create the InternalOrder, so that it returns an
InternalOrder but only declares that it returns an Order, as an
alternative.
 
eSapient said:
I faced a similar situation recently. I see two possibilities.

Your solution is one of them. Except, I would prefer to code like this:

Dim ord as new Order()
Dim IntOrd as InternalOrder() = ord.ConvertToInternalOrder()

I wouldn't - the derived class should know about the parent class, not
the other way round.
The other alternative is like this:

Dim ord as New Order()
Dim IntOrd as InternalOrder = New InternalOrder(ord)

That's better.

Alternatively, it sounds like a factory class would be a good solution
here, rather than clients directly creating the instances in the first
place.
 
Clayton Gulick said:
The problem is I don't want to have to write copy constructors for
each of the classes I am exposing, they are big and that would take
forever. I am looking for a generic class I can use to solve the
issue for *any* class I might want to force down.

Otherwise I have to remember to maintain all the copy constructors
any time a prop is added or deleted etc... and it starts to become a
management nightmare.

One alternative is to not have InternalOrder derive from Order at all,
but let it *contain* an order. That way you wouldn't need any copy
constructors at all.
 
Back
Top