我尝试使用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
表示为Number
inCallViewModel
被 覆盖Customer.Number
。两者CustomerName
并OperationTimeFrom
永远不会获得其指定来源的值Customer.Name
和Operation.TimeFrom
。
在那种情况下我该怎么办?ValueInjecter 甚至支持这种情况吗?