Best practice, and check for validity

Jan 31, 2011 at 3:47 PM

Hi,

I want to use Value Injecter to merge two classes  (type A and B both with an id property) in a third class (with two id properties : A_ID and B_ID).  This is my solution :

public class ConvertFromA:LoopValueInjection
{
protected override string TargetPropName(string sourcePropName)
{
if (sourcePropName == "id")
{
return "A_ID";
}
return base.TargetPropName(sourcePropName);
}
}

(and another one for B, just replace A per B :) )

Is this the best way ?


Also, there is a nice method in AutoMapper, called
Mapper.AssertConfigurationIsValid
which checks that "all of the properties on the destination type are matched up with something on the source type".

Is there something like that in Value injecter ?

thanks !
Coordinator
Feb 1, 2011 at 6:18 AM

hi,

you can do it like this in order to use 1 injection for many types:

       public class IdToType_ID<T> : LoopValueInjection
        {
            protected override bool UseSourceProp(string sourcePropName)
            {
                return sourcePropName == "id";
            }
            protected override string TargetPropName(string sourcePropName)
            {
                return typeof(T).Name + "_ID";
            }
        }

usage:
            foobar.InjectFrom<IdToType_ID<Foo>>(foo);
            foobar.InjectFrom<IdToType_ID<Bar>>(bar);
that method ensures that the configuration for each pair of possible and registered mappings is valid, vi doesn't require configuration
but you can write unit tests to test the valueinjections just like you would write for automapper to test valueresolvers or any other stuff that is not covered by that method

Cheers,

Omu