简体   繁体   中英

Handle same Object from different (Web)services

In my project I'm using 2 Webservices (lets call em servA and servB ) which return the same type of object.

The object looks as follows:

public class Record
{
        public String RecordName{ get; set; }
        public List<RecordAttribute> RecordAttributes{ get; set; }
}

public class RecordAttribute
{
        public String AttributeName{ get; set; }
        public List<object> RecordAttributesValues{ get; set; }
}

I've written a generic piece of code to map those RecordAttributes to a local Objecttype. But the problem i'm facing is I have to keep seperate functions for the results coming from servA Aand servB.

eg as follows:

for ServA

public List<T> mapServAResultToObject<T>(ServA.Record[] results)
{ 
     .... 
     mapServAAttributeToObject<T>(results.RecordAttributes)
     ....
}
Public T mapServBAttributesToObject<T>(ServA.RecordAttribute Attrs)
{ .... }

for servB

public List<T> mapServBResultToObject<T>(ServB.Record[] results)
{ 
     .... 
     mapServBAttributesToObject<T>(results.RecordAttributes)
     ....
}
Public T mapServBAttributesToObject<T>(ServB.RecordAttribute Attrs)
{ .... }

These double function are starting to hurt my eyes. Question:

  1. Is there a way around this, that i can just use a single function for both the results of ServA and ServB
  2. Or is there a performance-wise efficient way to cast those results to a fast localobject?

And this without adaptations to the services?

This is mapping example done with AutoMapper. It can map ServA and ServB objects into ObjectType. You can get AutoMapper from Nuget.

  class Program
    {
        static void Main(string[] args)
        {
            var objectFromServiceA = new ServA_Record();
            objectFromServiceA.RecordAttributes = new List<ServA_Record.ServA_RecordAttribute>();
            objectFromServiceA.RecordName = "Test A";
            objectFromServiceA.RecordAttributes.Add(new ServA_Record.ServA_RecordAttribute() { AttributeName = "Record Attr A", RecordAttributesValues = new List<object>() });

            var objectFromServiceB = new ServB_Record();
            objectFromServiceB.RecordAttributes = new List<ServB_Record.ServB_RecordAttribute>();
            objectFromServiceB.RecordName = "Test B";
            objectFromServiceB.RecordAttributes.Add(new ServB_Record.ServB_RecordAttribute() { AttributeName = "Record Attr B", RecordAttributesValues = new List<object>() });

            var businessObject = new ObjectType();

            Mapper.CreateMap<ServA_Record, ObjectType>();
            Mapper.CreateMap<ServB_Record, ObjectType>();
            Mapper.CreateMap<ServA_Record.ServA_RecordAttribute, ObjectType.ObjectTypeAttribute>();
            Mapper.CreateMap<ServB_Record.ServB_RecordAttribute, ObjectType.ObjectTypeAttribute>();

            businessObject = Mapper.Map<ServA_Record, ObjectType>(objectFromServiceA);
            Console.WriteLine(businessObject.RecordName);
            Console.WriteLine(businessObject.RecordAttributes[0].AttributeName);

            businessObject = Mapper.Map<ServB_Record, ObjectType>(objectFromServiceB);
            Console.WriteLine(businessObject.RecordName);
            Console.WriteLine(businessObject.RecordAttributes[0].AttributeName);

            Console.ReadKey();
        }
    }

    public class ServA_Record
    {
        public String RecordName { get; set; }
        public List<ServA_RecordAttribute> RecordAttributes { get; set; }

        public class ServA_RecordAttribute
        {
            public String AttributeName { get; set; }
            public List<object> RecordAttributesValues { get; set; }
        }
    }

    public class ServB_Record
    {
        public String RecordName { get; set; }
        public List<ServB_RecordAttribute> RecordAttributes { get; set; }

        public class ServB_RecordAttribute
        {
            public String AttributeName { get; set; }
            public List<object> RecordAttributesValues { get; set; }
        }
    }

    public class ObjectType
    {
        public String RecordName { get; set; }
        public List<ObjectTypeAttribute> RecordAttributes { get; set; }

        public class ObjectTypeAttribute
        {
            public String AttributeName { get; set; }
            public List<object> RecordAttributesValues { get; set; }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM