处理来自不同(Web)服务的相同Object

时间:2015-01-19 14:48:23

标签: c# web-services

在我的项目中,我使用2个Webservices(允许调用em servAservB),它们返回相同类型的对象。

该对象如下所示:

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; }
}

我编写了一段通用代码来将这些RecordAttributes映射到本地Objecttype。但我面临的问题是I have to keep seperate functions for the results coming from servA Aand servB.

e.g。如下:

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)
{ .... }

这些双重功能开始伤害我的眼睛。 的问题:

  1. 有没有解决方法,我可以使用单一功能同时使用ServA和ServB的结果
  2. 或者是否有一种以性能为导向的高效方式将这些结果转换为快速的localobject?
  3. 这没有改编服务吗?

1 个答案:

答案 0 :(得分:1)

这是使用AutoMapper完成的映射示例。它可以将ServA和ServB对象映射到ObjectType。你可以从Nuget获得AutoMapper。

  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; }
        }
    }