Basically with the ValueInjecter you map (inject) values from one source object into another target object using mapping algorithms called ValueInjection

Let say that you have this 2 classes
        public class Foo
        {
            public int Id { get; set; } 
            public string Name { get; set; }
            public object Prop1 { get; set; }
            public string Abc { get; set; }
        }

        public class Bar
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public object Prop1 { get; set; }
            public string SomeProp { get; set; }
        }

and you would like to map the values of the Properties that are of the same name and type from an instance of Foo to an instance of Bar, than you would do it like this:

var foo = new Foo { Id = 1, Name = "aname", Prop1 = 321 , Abc = "abc" };
var bar = new Bar { SomeProp = "somevalue" };

bar.InjectFrom(foo);
//now bar is Id = 1, Name = "aname" Prop1 = 321 and SomeProp = "somevalue" 
in here the LoopValueInjection it is used which is the default ValueInjection,


so you can have the same result by calling
bar.InjectFrom<LoopValueInjection>(foo);


Now you can create your own ValueInjections by extending one of the following classes:
  • LoopValueInjection - maps the properties with the same name (you have the possibility to indicate target/source prefix e.g. from txtName <-> Name ) and type
  • LoopValueInjection<TSourcePropertyType, TTargetPropertyType> - maps the properties with the same name from the source object of type TSourcePropertyType to the ones from the target object of type TTargetPropertyType,


Examples of extending the LoopValueInjection<TSourceProperty, TTargetProperty>

       public class StringToInt : LoopValueInjection<int, string>
        {
            protected override string SetValue(int sourcePropertyValue)
            {
                return sourcePropertyValue.ToString();
            }
        }

        public class IntToBar : LoopValueInjection<int, Bar>
        {
            protected override Bar SetValue(int sourcePropertyValue)
            {
                return new BarRepository().Get(sourcePropertyValue);
            }
        }

and usage:
a.InjectFrom<StringToInt>(b);
  .InjectFrom<IntToBar>(b);

Last edited Jun 15, 2010 at 1:03 PM by o, version 9

Comments

No comments yet.