Customize injecter behaviour

Jan 19, 2013 at 1:15 PM
Edited Jan 19, 2013 at 1:17 PM

I'm new to the Value Injecter and have a question.
I want to customize the injecter behaviour for PropB in the following scenario:

class ObjA
{
	public int PropA { get; set; }
	public SomeType PropB { get; set; }
}

class ObjB
{
	public int PropA { get; set; }
	public SomeOtherType PropB { get; set; }
}

ObjA a = new ObjA();
ObjB b = new ObjB();
b.InjectFrom<ObjMapper>(a);

For the implementation of ObjMapper, I first tried:

class ObjMapper : LoopValueInjection<ObjA, ObjB>
{
	protected override ObjB SetValue(ObjA sourcePropertyValue)
	{
	}
}

but that didn't work at all. SetValue was never called (only to be used for simple types?)
Next, I tried:

class ObjMapper : LoopValueInjection
{
	protected override void Inject(object source, object target)
	{
		ObjA a = source as ObjA;
		ObjB b = target as ObjB;

		base.Inject(source, target);
		b.PropB = <Some fancy code>
	}
}

This works, but it's messy and ugly with the casting.
What I really want is:

class ObjMapper : LoopValueInjection<ObjA, ObjB>
{
	protected override void Inject(ObjA source, ObjB target)
	{
		base.Inject(source, target);
		target.PropB = <Some fancy code>
	}
}

Nice and clean, with typed parameters.

Have I misunderstood what class to derive from in this scenario or is the above just not possible?

Coordinator
Jan 19, 2013 at 3:35 PM

try the new ConventionInjection

look here first: http://valueinjecter.codeplex.com/wikipage?title=step%20by%20step%20explanation&referringTitle=Home