let's say you have this 2 classes:
        public class Foo
        {
            public Foo Foo1 { get; set; }
            public Foo Foo2 { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
        }

        public class FlatFoo
        {
            public string Foo1Foo2Foo1Name { get; set; }
            public string Foo1Name { get; set; }
            public string Foo2Name { get; set; }
            public string Foo1Age { get; set; }
        }

you can map them like this:

           var unflat = new Foo
                             {
                                 Foo1 = new Foo
                                            {
                                                Name = "abc",
                                                Foo2 = new Foo { Foo1 = new Foo { Name = "inner" } }
                                            },
                                 Foo2 = new Foo { Name = "123" },
                             };

            var flat = new FlatFoo();

            flat.InjectFrom<FlatLoopValueInjection>(unflat);

//flat.Foo1Foo2Foo1Name = "inner"
//flat.Foo1Name = "abc"
//flat.Foo2Name = "123"

you can also extend the FlatLoopValueInjection or FlatLoopValueInjection<TSourceProperty, TTargetProperty> (to flat from one type to another) , example:
        public class IntToStringFlat : FlatLoopValueInjection<int, string>
        {
            protected override string SetValue(int sourceValue)
            {
                return sourceValue.ToString();
            }
        }

Last edited Jun 14, 2010 at 11:07 AM by o, version 2

Comments

No comments yet.