16 May 2014

A ValueInjecter Injection that ignores null values

Want to use ValueInjecter but don't want to copy null values into your model?

public class NoNullsInjection : ConventionInjection
{
    protected override bool Match(ConventionInfo c)
    {
        return c.SourceProp.Name == c.TargetProp.Name
                && c.SourceProp.Value != null;
    }
}

Usage:

target.InjectFrom(new NoNullsInjection(), source);
If I helped you out today, you can buy me a beer below. Cheers!