Getting started...

Basically with the ValueInjecter you inject values from the properties of one source object into properties of another target object using mapping algorithms called ValueInjections
Value Injections are used to specify the algorithm for matching the properties and value transformation from source to target

Usage:

import the ValueInjecter namespace:
using Omu.ValueInjecter;

use it with it's default behavior or with your custom ValueInjections
// inject all the values from the properties of the same name and type from foo into bar
bar.InjectFrom(foo); 

bar.InjectFrom<MyValueInjection>(foo);

bar.InjectFrom(new MyInjectionWithParams(a,b,c), foo);

Example

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

        public int Country1 { get; set; }
        public int Country2 { get; set; }
    }
    
    public class PersonDto
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public Country Country1 { get; set; }
        public Country Country2 { get; set; }
    }

   public class PersonDisplay
   {
        public string Name { get; set; }
   }

and that's how you map them:
var person = new Person{ Id = 3, Name = "Danny", Country1 = 7, Country2 = 9 };

var personDto = new PersonDto();

personDto.InjectFrom(person) // this does the props of the same name and type (Id and Name)
       .InjectFrom<IntToCountry>(person); // from person's props of type int to personDto's props of type Country (Country1 and Country2)
  
var personDisplay = new PersonDisplay();
personDisplay.InjectFrom(person); // inject from props of same name and type

this is the ValueInjection that you need to write for the above ^ to work
    // it will match the props with the same name that are of type int on one side and of type Country on the other side
    public class IntToCountry : LoopValueInjection
    {
        //by default is sourceType == targetType; override to change behaviour
        protected override bool TypesMatch(Type sourceType, Type targetType)
        {
            return sourceType == typeof(int)
                   && targetType == typeof(Country);
        }
        
        //by default is return sourcePropertyValue; override to change behaviour 
        protected override object SetValue(object sourcePropertyValue)
        {
            // this is just a sample, but you could write anything here
            return new Country 
            { 
                Id = (int)sourcePropertyValue, 
                Name = "Country" + sourcePropertyValue 
            }
        }
    }

Some useful Loop value injections:


        public class EnumToInt : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.SourceProp.Name == c.TargetProp.Name &&
                    c.SourceProp.Type.IsSubclassOf(typeof (Enum)) && c.TargetProp.Type == typeof (int);
            }
        }      
     
        public class IntToEnum : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.SourceProp.Name == c.TargetProp.Name &&
                    c.SourceProp.Type == typeof (int) && c.TargetProp.Type.IsSubclassOf(typeof (Enum));
            }
        }

        //e.g. int? -> int
        public class NullablesToNormal : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.SourceProp.Name == c.TargetProp.Name &&
                       Nullable.GetUnderlyingType(c.SourceProp.Type) == c.TargetProp.Type;
            }
        }
        //e.g. int -> int?
        public class NormalToNullables: ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.SourceProp.Name == c.TargetProp.Name &&
                       c.SourceProp.Type == Nullable.GetUnderlyingType(c.TargetProp.Type);
            }
        } 
      

in case you need to send parameters to your injection, you can call it like this:
//in case you extend it and you also add some parameters in the constructor you can call it like this
bar.InjectFrom(new MyInjection(a, b, 32, someService), foo);

Last edited Feb 3, 2011 at 12:04 PM by o, version 41

Comments

dean_goddard Jul 8, 2015 at 11:32 AM 
As far as I can tell, ConventionInjection is now obsolete, but there is no forth coming documentation on how best to replace it. What's the difference between LoopInjection and UnflatLoopInjection without having to read the code? Love Value Injector, but the documentation is now out of date and it's a wee bit frustrating. :(