Search code examples
c#wcfdata-structuresdatacontractdata-layers

Handling different classes of the same name in a single CS file


I'm using two WCF services that declare a class Donkey each. Those Donkey classes, although identical in structure, aren't the same type, due to namespace issues.

This far I'm using using (as described f.i. here), but I feel that I'd like to get it under some more strict control.

Any suggestions on how (and where) to place a data contract converter that maps the both Donkey classes to each other? I know that it's a bit goofy question. It's got to do with the fact that I'm not sure if that's even possible. Feel free to correct my formulation.

All suggestions are welcome. I'm considering writing my own data definition layer and create my own, better Donkey objects (with booze and hookers, if one dares to use a reference to Futurama's Bender).


Solution

  • If I understand your situation correctly, you have two identical structures of different types that you want to use interchangeably. In this case, all you need is to define implicit casts. So, in one definition of Donkey, define a cast to and from the other Donkey.

    If you do not have control over the two Donkeys or you would prefer to keep the classes decoupled, then adding a third Donkey class would make more sense. In this class, you would define the casts from the other Donkeys.

    Depending on the details of your situation, you should try to use just one of the Donkeys as much as you can throughout your project, and only use the other when necessary.