WCF和嵌套数据契约

时间:2013-02-14 00:33:33

标签: wcf

WCF全新。真的可以使用一些帮助。

我有一个(100)对象列表,有4个数据成员。 DM1,DM2,DM3,DM4

我有一组datacontracts

DC1
 List<DC2>

DC2
 <DM1 Value=n> (n could be any number below 5, so there could be up to 5 DC2 inside the List in DC1)
 List<DC3>

DC3
 <DM2 value=n> (n could be any number below 10, so up to 10 DC3 inside the List in DC2)
 List<DC4>

DC4
 <DM3>
 <DM4>

需要遍历原始对象列表,并使用各种嵌套数据对象创建一个datacontract(DC1),具体取决于原始对象列表中数据成员的不同值。

e.g:

<DC1>
  <DC2>
    <DM1 value = "a">
    <DC3>
      <DM2 value = 1>
      <DC4>
        <DM3>
        <DM4>
      <DM2 value = 2>
      <DC4>
        <DM3>
        <DM4>
      <DC4>
        <DM3>
        <DM4>
    </D3>
  </DC2>
  <DC2>
    <DM1 value = "b">
    <DC3>
      <DM2 value = 1>
      <DC4>
        <DM3>
        <DM4>
      <DM2 value = 2>
      <DC4>
        <DM3>
        <DM4>
      <DC4>
        <DM3>
        <DM4>
    </D3>
  </DC2>
<DC1>

这样做的最佳方式是什么?

谢谢!

这是DataContracts以及使用DC1传递信息的服务合同:

[DataContract]
public class DC1
{
    [DataMember]
    public string string { get; set; }

    [DataMember]
    public List<DC2> LDC2{ get; set; }
}

[DataContract]     公共课DC2     {         [数据成员]         public string Type {get;组; }

    [DataMember]
    public List<DC3> DC3s{ get; set; }

[DataContract]     公共课DC3     {         [数据成员]         public decimal num {get;组; }

    [DataMember]
    public List<DC4> DC4s{ get; set; }

[DataContract]     公共课DC4     {         [数据成员]         public int num2 {get;组; }

    [DataMember]
    public decimal Val{get; set;}

服务合同: [ServiceContract(Namespace = Constants.Namespace)]     公共课堂服务     {

    [OperationContract]
    DC1 GetMethod(int num);

数据集:

是LIST&lt;&gt;以下对象:

[Serializable]
public class Data
{
    public string Type { get; set; }
    public double Num { get; set; }
    public double Num2{ get; set; }
    public decimal Val{ get; set; }

}

此列表中可以包含100个(或更多)这些对象。 最多5种类型,最多10个Num,最多5个Num2。

感谢您的回复,并希望这澄清!

1 个答案:

答案 0 :(得分:1)

可能比让LINQ完成所有工作更有效。但是,这似乎是关于如何将非规范化数据转换为规范化对象的问题,因此GroupBy跳出来是一个易于编码的解决方案。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace WcfTestService
{
    [ServiceContract]
    public interface ISoapService
    {
        [OperationContract]
        DC1 GetDC1();
    }

    public class SoapService : ISoapService
    {
        public DC1 GetDC1()
        {
            // Example dataset
            var dataset = new List<Data>()
            {
                new Data() { Type = "a", Num = 1, Num2 = 1, Val = 41.00m },
                new Data() { Type = "a", Num = 2, Num2 = 1, Val = 42.00m },
                new Data() { Type = "a", Num = 2, Num2 = 2, Val = 43.00m },
                new Data() { Type = "b", Num = 1, Num2 = 1, Val = 44.00m },
                new Data() { Type = "b", Num = 2, Num2 = 1, Val = 45.00m },
                new Data() { Type = "b", Num = 2, Num2 = 2, Val = 46.00m },
            };

            // Process dataset into data contract objects
            return new DC1()
            {
                DC2s = dataset.GroupBy(x => x.Type).Select(typeGrouping => new DC2()
                {
                    DM1 = typeGrouping.Key,
                    DC3s = typeGrouping.GroupBy(x => x.Num).Select(numGrouping => new DC3()
                    {
                        DM2 = Convert.ToDecimal(numGrouping.Key),
                        DC4s = numGrouping.Select(x => new DC4()
                        {
                            DM3 = Convert.ToInt32(x.Num2),
                            DM4 = x.Val
                        }).OrderBy(x => x.DM3).ToList()
                    }).OrderBy(x => x.DM2).ToList()
                }).OrderBy(x => x.DM1).ToList()
            };
        }
    }

    [Serializable]
    public class Data
    {
        public string Type { get; set; }
        public double Num { get; set; }
        public double Num2 { get; set; }
        public decimal Val { get; set; }
    }

    [DataContract]
    public class DC1
    {
        [DataMember]
        public List<DC2> DC2s { get; set; }
    }

    [DataContract]
    public class DC2
    {
        [DataMember]
        public string DM1 { get; set; }
        [DataMember]
        public List<DC3> DC3s { get; set; }
    }

    [DataContract]
    public class DC3
    {
        [DataMember]
        public decimal DM2 { get; set; }
        [DataMember]
        public List<DC4> DC4s { get; set; }
    }

    [DataContract]
    public class DC4
    {
        [DataMember]
        public int DM3 { get; set; }
        [DataMember]
        public decimal DM4 { get; set; }
    }
}

GetDC1()的输出应该非常接近您的示例输出,但是所有WCF都已完成。