Reputation: 15906
I have a WCF service interface that looks like this:
[ServiceContract(Namespace = "http://somenamespace", SessionMode = SessionMode.NotAllowed)]
public interface IRuntimeService
{
[OperationContract]
ISupporterProperties CreateOrModifySupporter(Guid id);
}
And the implementation (both on the client and the server) looks like this (it is hosted and connected to programmatically):
public IOwnerProperties CreateOrModifyOwner(Guid id)
{
//get an owner ...
//the owner is of type Owner which implements IOwnerProperties.
return theOwner;
}
However, the issue is here that WCF will try to serialize or deserialize this as an Owner
since that is the actual type being returned, but I would like it to send it over as a OwnerDataContract
which also happens to implement IOwnerProperties
.
In other words, I want to return an Owner
, but make it serialize/deserialize it as a OwnerDataContract
.
I am aware that I can create a wrapper class for the client's interface. However, I would like to have as much shared code as possible.
Upvotes: 0
Views: 58
Reputation: 127603
This is perfect job for AutoMapper. If Owner
and OwnerDataContract
have the same properties and fields the setup is as simple as
static void Main()
{
//Do this once at program startup
Mapper.CreateMap<Owner, OwnerDataContract>();
//Run the rest of your program.
}
If you need to remap some properties due to flattening or renaming there will be more setup work, see the wiki for more info.
To use the mapping it is as simple as
public IOwnerProperties CreateOrModifyOwner(Guid id)
{
//get an owner ...
//Mapper.Map retuns a object of type OwnerDataContract
return Mapper.Map<OwnerDataContract>(theOwner);
}
Upvotes: 1