I’m working a business layer (separate assembly) that will contain various business entities used in multiple client applications. One of the client applications is a WCF app that will need to serialize these entities. In order to properly serialize the entities, metadata for data contracts or for XML serialization must be added, but the serialization requirements may vary across applications.
Clearly I need the metadata separated from the class definition. So how do I add metadata to my entities for each separate application?
In discussion of WCF you are essentially talking about a DTO. It would be a fair argument to say that if the serialization is different, then it isn’t the same DTO, since the entire purpose of the DTO is to allow appropriate serialisation. So my answer would be: have multiple parallel DTO classes representative of the needs of the different APIs, and translate between them.
You might be able to work some magic by specifying the serializer at runine, but that is more brittle and hard to configure.