Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize 3rd party class?

Tags:

c#

.net

wcf

I have a WCF service that is using a 3rd party library. The library is not serializable.

I have a custom type that is serializable and also includes a class from the 3rd party library as a property. It looks like:

MyClass.ThirdPartyClass

The problem is when I send MyClass out of the service, ThirdPartyClass looses the values I had assigned it while in the service. Since I don't have the source for ThirdPartyClass, I can mark it as serializable.

Is there some way to mark ThirdPartyClass as serializable or some other way to have it retain values?

like image 882
4thSpace Avatar asked Nov 05 '13 21:11

4thSpace


1 Answers

I'd suggest to create a data transfer object that contains the data that you want to provide through your service. Instead of making the ThirdPartyClass part of MyClass, use the new DTO and map the data of the ThirdPartyClass to the DTO before returning it from the Service. Maybe a Framework like AutoMapper can support you in this task.
Though this means some extra effort, it also creates a layer of abstraction between the clients of your service and the 3rd party library. This enabbles you to change to another library later on without having to change the interface of your service.

like image 118
Markus Avatar answered Sep 20 '22 06:09

Markus