Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a WPF client which calls WCF service, that service again calls another wcf service which is residing in another location (WPF --> WCF Service1(dummy) --> Wcf Service2 (Another location)). I can't directly call service 2 from WPF due to design, WPF client should not be exposed, so I am taking this route.

Now, Service 2 returns a data contract to service1 as response object. I have to convert that data contract in service 1 and send the response back to WPF client. I created same datacontract in service1 with exact same properties. Does any one know any way to convert the custom type, I thought of looping and assigning each property manually, but I believe there would be some better solution. Could any one suggest with some example available please.

Thanks in advance.
Posted

1 solution

First of all, the need in "conversion" between class members (you do not "convert" types, you "convert" the instances) is a pretty sure sign of a flaw in the code design.

If two types are not related (one is not derived from another directly or indirectly) you cannot just make a cast between them. All you can do is to create a brand new instance of the target type and copy all data from source instance. Moreover, you may or may not need to deep-clone some data. Please see my past answer, by the way:
How to implement ICloneable for a List Class?[^].

For some syntactic sugar, you can develop a type conversion operator in one or another type. Please see:
http://msdn.microsoft.com/en-us/library/85w54y0a.aspx[^].

See also:
http://en.wikipedia.org/wiki/Deep_copy#Deep_copy[^],
http://en.wikipedia.org/wiki/Syntactic_sugar[^].

But first things first. I repeat, first thing would be to review your architecture and design of your data structures, it can be a big flaw.

—SA
 
Share this answer
 
Comments
sam3440 18-Oct-13 16:09pm    
Can you suggest any Serializer which convert one class instance to another class instance.
Sergey Alexandrovich Kryukov 18-Oct-13 16:23pm    
Why serializer? Anyway, if you need to use a serializer, it's better be Data Contract (and then you can achieve matching name and "automate" the process), as I demonstrated in my answer referenced above. But, as I say, this is ugly. You really want to review your architecture and data structures.
—SA
sam3440 18-Oct-13 16:29pm    
I don't understand why this is ugly, because I don't want my client to call external service directly. So instead I am creating dummy service and making call with that service to external service (WPF --> WCF Service1(dummy) --> Wcf Service2 (Another location)). I looking for serializer because, WcfService 2 returns response object (class name: FormList) so I want to convert that response object in Service 1 and send it back to client. If I send the response directly, then I am indirectly exposing client to service2
Sergey Alexandrovich Kryukov 18-Oct-13 16:37pm    
Well, and I don't understand why do you have unrelated classes with same or semantically close members. And you are telling me it can possibly may be not ugly? Well, well...
Anyway, I answered your question in full, will you accept it formally (green "Accept" button)?
—SA
sam3440 18-Oct-13 16:56pm    
thank you for the response

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900