IT:AD:ValueInjecter
Example
An example:
public struct TypeKey
{
private Type _t1;
public Type T1
{
get { return _t1; }
set { _t1 = value; }
}
private Type _t2;
public Type T2
{
get { return _t2; }
private set { _t2 = value; }
}
public TypeKey(Type t1, Type t2)
{
_t1 = t1;
_t2 = t2;
}
}
public interface IConventionMappingService
{
T2 MapAway<T1, T2>(T1 t1) where T2 : class, new();
T2 MapAway<T1, T2, TConverter>(T1 t1)
where T2 : class, new()
where TConverter : ConventionInjection, new();
void Register(Type t1, Type t2, ConventionInjection mapping);
}
public class ConventionMappingService :IConventionMappingService
{
private Dictionary<TypeKey, ConventionInjection> _conventionInjections =
new Dictionary<TypeKey, ConventionInjection>();
public T2 MapAway<T1, T2>(T1 t1)
where T2 : class, new()
{
return new T2().InjectFrom(t1) as T2;
}
public T2 MapAway<T1, T2, TConverter>(T1 t1)
where T2 : class, new()
where TConverter : ConventionInjection, new()
{
return new T2().InjectFrom<TConverter>(t1) as T2;
}
public void Register(Type t1, Type t2, ConventionInjection mapping)
{
_conventionInjections[new TypeKey(t1, t2)] = mapping;
}
}
The MappingService tries to map from one to another based on conventions.
There are built in ones, which is fine for a while, but you can register your own
Conventions (which the documentation refers to as Injections).
Here's an example a trivial convention:
//Create a custom Converter, based on ConvetionInjection
//specifying a convention based on src and target property names:
public class MyConverter : ConventionInjection
{
protected override bool Match(ConventionInfo c)
{
return c.SourceProp.Name == "Name" && c.TargetProp.Name == "Title";
// where source prop is "Id" and target prop is Source type name + "_Id" ( Id - Foo_Id; Id - Bar_Id )
}
}
See IT:AD:ValueInjecter:HowTo:Convention Examples for more examples.