2

我尝试使用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表示为NumberinCallViewModel被 覆盖Customer.Number。两者CustomerNameOperationTimeFrom永远不会获得其指定来源的值Customer.NameOperation.TimeFrom

在那种情况下我该怎么办?ValueInjecter 甚至支持这种情况吗?

4

1 回答 1

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; }
        }
    }
}
于 2012-12-11T10:54:06.480 回答