Search code examples
c#valueinjecter

Flattening with ValueInjecter


I try to map multiple objects to a view model object with ValueInjecter. But it does not work as I expect it.

Code:

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();
    }
}

Expected Output:

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

Effective Output:

Number:            00001
CustomerName:      
OperationTimeFrom: 

Notice

The Call.Number, represented as Number in CallViewModel gets overwritten by Customer.Number. Both CustomerName and OperationTimeFrom never get the value of its designated source Customer.Name and Operation.TimeFrom.

What do I have to do in that scenario? Does ValueInjecter even support this scenario?


Solution

  • I guess you thought of flattening in a different way, the way it works is that you go from

    obj.Prop.Prop1.Prop2 to obj.PropProp1Prop2
    

    this is your code modified:

    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; }
            }
        }
    }