使用ValueInjecter展平

时间:2012-12-06 09:15:38

标签: c# 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中表示为Number的{​​{1}}被CallViewModel覆盖。 Customer.NumberCustomerName都不会获得指定来源OperationTimeFromCustomer.Name的值。

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

1 个答案:

答案 0 :(得分: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; }
        }
    }
}