Search code examples
c#valueinjecter

Valueinjecter mapping complex objects


I have this simple code:

using Omu.ValueInjecter;

namespace InjectionTest
{
    public class Partner
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public City City { get; set; }
    }

    public class City
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class PartnerViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string CityName { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var city = new City()
            {
                Id = 1,
                Name = "Athens"
            };

            var partner = new Partner()
            {
                Id = 1,
                Name = "Acme d.o.o.",
                City = city
            };

            var vm = new PartnerViewModel();

            vm.InjectFrom(partner);

        }
    }
}

As I understand Valueinjector maps complex object graphs by class name + property name. I expected the vm object property CityName to be "Athens" after the injection, but it is null. I guess I am missing something. Help appreciated.


Solution

  • Never mind. After going through the samples I realized that the line

    vm.InjectFrom(partner);
    

    should read

    vm.InjectFrom<FlatLoopInjection>(partner);
    

    I was under the impression that this should be the default, but seems not.