Using interfaces to convert data between classes - Web Services

G

Guest

Hello!

I've got an application that uses web services to traverse between various
tiers, and we're passing complex data types back and forth as both parameters
and return types.

Originally, I was serializing the complex types into a string, and then
passing that around, but we're investigating how much of a performance hit
(if any) that is causing us.

This is a rather simple question regarding interfaces, actually, and it all
boils down to this. How can I cast an object that inherits an interface into
another object that inherits the interface, without using an explicit
conversion with an adapter class or customized constructor? Let's say I have
the following defined...

Public Interface IFruit
Property Price() As Double
Property Color() As String
End Interface

Public Class Apple
Implements IFruit
Public Property Color() As String Implements IFruit.Color
Get
Return pColor
End Get
Set(ByVal Value As String)
pColor = Value
End Set
End Property
Public Property Price() As Double Implements IFruit.Price
Get
Return pPrice
End Get
Set(ByVal Value As Double)
pPrice = Value
End Set
End Property
Private pPrice As Double
Private pColor As String
End Class

Public Class Orange
Implements IFruit
Public Property Color() As String Implements IFruit.Color
Get
Return pColor
End Get
Set(ByVal Value As String)
pColor = Value
End Set
End Property
Public Property Price() As Double Implements IFruit.Price
Get
Return pPrice
End Get
Set(ByVal Value As Double)
pPrice = Value
End Set
End Property
Private pPrice As Double
Private pColor As String
End Class

I'd basically like to be able to do this:

Dim fred As New Apple
fred.Price = 0.89
fred.Color = "red"
Dim larry As Orange
larry = CType(fred, Orange)

I know that I can do:

larry = Ctype(fred, IFruit)

But it's really preferable to have it casted as an Orange. Obviously the
real life class and interface design is significantly more complicated, but
is there a way I can essentially do this, without writing a constructor for
Orange that accepts an "Apple" object or some kind of adapter?

Thanks for any input!

John
 
L

Larry Lard

John Cortright wrote:
[snip]
This is a rather simple question regarding interfaces, actually, and it all
boils down to this. How can I cast an object that inherits an interface into
another object that inherits the interface, without using an explicit
conversion with an adapter class or customized constructor? Let's say I have
the following defined...

Public Interface IFruit
Property Price() As Double
Property Color() As String
End Interface

Public Class Apple
Implements IFruit ....
End Class

Public Class Orange
Implements IFruit
....
End Class

I'd basically like to be able to do this:

Dim fred As New Apple
fred.Price = 0.89
fred.Color = "red"
Dim larry As Orange
larry = CType(fred, Orange)

I know that I can do:

larry = Ctype(fred, IFruit)

But it's really preferable to have it casted as an Orange. Obviously the
real life class and interface design is significantly more complicated, but
is there a way I can essentially do this, without writing a constructor for
Orange that accepts an "Apple" object or some kind of adapter?

No, you are going to have to explicitly specify in some way the
semantics of how an Orange is created from an Apple. Consider a
different example of the same thing: Hashtable and Queue both implement
ICollection, but you wouldn't expect there to be a straightforward way
of converting one to the other without going into some detail about how
to do it, would you?

As you mention, writing a constructor for Orange that takes an IFruit
(not specifically an Apple, mind) is the way to go here.

I imagine that in your actual scenario the conversion makes more sense
than converting an Apple to an Orange, btw! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top