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
,表示爲CallViewModel
Number
得到由Customer.Number
覆蓋。 CustomerName
和OperationTimeFrom
都不會得到其指定來源Customer.Name
和Operation.TimeFrom
的值。
在這種情況下我該做些什麼? ValueInjecter甚至支持這種情況嗎?
我試過,然後再發布我的題。我使用了'viewModel.InjectFrom(call,op,customer);'但以上面的輸出結束 –
Sandro
@Sandro I編輯了我的答案 – Omu
好了我現在明白'FlatLoopValueInjection'尋找匹配的屬性名稱。是否有一個匹配類型名稱的注入?如果沒有,是否有可能創建一個? – Sandro