Reputation: 439
These are third party web services so I have no control over them. They reuse elements so the structure is the same for a lot of the service, there are only slight differences. My data that I send to the different services is identical. However the services do have different purposes, and give me different responses.
For example:
private static service1.IdenticalObject createSomeElement(MyDataObject data)
{
service1.IdenticalObject theirServiceElement= new service1.IdenticalObject();
theirServiceElement.IdenticalField = data.IdenticalField;
return theirServiceElement;
}
private static service2.IdenticalObject createSomeElement(MyDataObject data)
{
service2.IdenticalObject theirServiceElement= new service2.IdenticalObject();
theirServiceElement.IdenticalField = data.IdenticalField;
return theirServiceElement;
}
There are many of these and some of them require hundreds of lines of identical code. Whey they update their services, I have to update the identical code in many different locations. I haven't been able to come up with a solution that only requires implementing the identical elements once, due to the fact that when it comes to add their element into the request, it requires the element with the specific namespace from that request.
EDIT: We use our data object with web services from other parties too, so I cannot specifically match the names of the our data fields to the service's. (see Automapper answer below)
Upvotes: 1
Views: 58
Reputation: 2100
Using AutoMapper might be a solution for you. It can be installed via NuGet.
AutoMapper will map (copy) the identical fields from one object to another.
The implementation might look like this:
private static service1.IdenticalObject createSomeElement(MyDataObject data)
{
service1.IdenticalObject theirServiceElement= new service1.IdenticalObject();
Mapper.Map(data, theirServiceElement);
return theirServiceElement;
}
private static service2.IdenticalObject createSomeElement(MyDataObject data)
{
service2.IdenticalObject theirServiceElement= new service2.IdenticalObject();
Mapper.Map(data, theirServiceElement);
return theirServiceElement;
}
In its simplest configuration, AutoMapper works by convention and copies the value of each member in the source object to the member of the same name in the destination object, if found.
So in your example, the value of data.IdenticalField would be copied to theirServiceElement.IdenticalField.
If they renamed IdenticalField in all their services to IdenticalItem, then you would just need to do the same rename within your MyDataObject class. Likewise if they add a new field, you would just need to add a field with the same name to MyDataObject.
For the above AutoMapper example to work, you would also need to add some configuration to create mappings between MyDataObject and each of their IdenticalObject classes.
That configuration might look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
namespace YourAppNamespace
{
public class AutoMapperConfig
{
public static void Configure()
{
Mapper.CreateMap<MyDataObject, service1.IdenticalObject>();
Mapper.CreateMap<MyDataObject, service2.IdenticalObject>();
}
}
}
And called from Global.asax like this:
protected void Application_Start()
{
AutoMapperConfig.Configure();
}
Unit tests can be created to test the AutoMapper configuration and the individual mappings.
Upvotes: 1