2012-12-06 49 views
2

我嘗試使用ValueInjecter將多個對象映射到視圖模型對象。但它不像我所期望的那樣工作。使用ValueInjecter進行展平

代碼:

class Call 
{ 
    public string Number { get; set; } 
    public string CustomerNumber { get; set; } 
} 

class Operation 
{ 
    public int ID { get; set; } 
    public string CallNumber { get; set; } 
    public string TimeFrom { get; set; } 
} 

class Customer 
{ 
    public string Number { get; set; } 
    public string Name { get; set; } 
} 

class CallViewModel 
{ 
    public string Number { get; set; } 
    public string CustomerName { get; set; } 
    public string OperationTimeFrom { get; set; } 
} 

class Test 
{ 
    static void Main(string[] args) 
    { 
     var call = new Call { Number = "001337", CustomerNumber = "000001" }; 
     var op = new Operation { ID = 1, CallNumber = "001337", TimeFrom = "08:00" }; 
     var customer = new Customer { Number = "000001", Name = "John Doe" }; 

     var viewModel = new CallViewModel(); 
     viewModel.InjectFrom(call, op, customer); 

     Console.WriteLine("Number:\t\t\t" + viewModel.Number); 
     Console.WriteLine("CustomerName:\t\t" + viewModel.CustomerName); 
     Console.WriteLine("OperationTimeFrom:\t" + viewModel.OperationTimeFrom); 
     Console.ReadLine(); 
    } 
} 

預期輸出:

Number:   001337 
CustomerName:  John Doe 
OperationTimeFrom: 08:00 

有效輸出:

Number:   00001 
CustomerName:  
OperationTimeFrom: 

通知

Call.Number,表示爲CallViewModelNumber得到由Customer.Number覆蓋。 CustomerNameOperationTimeFrom都不會得到其指定來源Customer.NameOperation.TimeFrom的值。

在這種情況下我該做些什麼? ValueInjecter甚至支持這種情況嗎?

回答

0

我猜你想以不同的方式扁平化的, 它的工作方式是,你從

obj.Prop.Prop1.Prop2 to obj.PropProp1Prop2 

去,這是修改代碼:

using System; 
using Omu.ValueInjecter; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     private static void Main(string[] args) 
     { 
      var call = new Call { Number = "001337", CustomerNumber = "000001" }; 
      var op = new Operation { ID = 1, CallNumber = "001337", TimeFrom = "08:00" }; 
      var customer = new Customer { Number = "000001", Name = "John Doe" }; 

      var model = new 
          { 
           Call = call, 
           Customer = customer, 
           Operation = op 
          }; 

      var viewModel = new CallViewModel(); 
      viewModel.InjectFrom<FlatLoopValueInjection>(model); 

      Console.WriteLine("Number:\t\t\t" + viewModel.CallNumber); 
      Console.WriteLine("CustomerName:\t\t" + viewModel.CustomerName); 
      Console.WriteLine("OperationTimeFrom:\t" + viewModel.OperationTimeFrom); 
      Console.ReadLine(); 
     } 

     class Call 
     { 
      public string Number { get; set; } 
      public string CustomerNumber { get; set; } 
     } 

     class Operation 
     { 
      public int ID { get; set; } 
      public string CallNumber { get; set; } 
      public string TimeFrom { get; set; } 
     } 

     class Customer 
     { 
      public string Number { get; set; } 
      public string Name { get; set; } 
     } 

     class CallViewModel 
     { 
      public string CallNumber { get; set; } 
      public string CustomerName { get; set; } 
      public string OperationTimeFrom { get; set; } 
     } 
    } 
} 
+0

我試過,然後再發布我的題。我使用了'viewModel.InjectFrom (call,op,customer);'但以上面的輸出結束 – Sandro

+0

@Sandro I編輯了我的答案 – Omu

+0

好了我現在明白'FlatLoopValueInjection'尋找匹配的屬性名稱。是否有一個匹配類型名稱的注入?如果沒有,是否有可能創建一個? – Sandro