18 Jan 2012

ValueInjecter - matching nullable to non-nullable values

Here's a little ValueInjecter plugin I wrote to handle ValueInjecter matching for properties where the name matches but one is nullable and the other is not.
        
        public class NullableInjection : ConventionInjection
        {
            protected override bool Match(ConventionInfo c)
            {
                return c.SourceProp.Name == c.TargetProp.Name &&
                       (c.SourceProp.Type == c.TargetProp.Type 
                        || c.SourceProp.Type == Nullable.GetUnderlyingType(c.TargetProp.Type)
                        || (Nullable.GetUnderlyingType(c.SourceProp.Type) == c.TargetProp.Type 
                                && c.SourceProp.Value != null )
                        );
            }

            protected override object SetValue(ConventionInfo c)
            {
                return c.SourceProp.Value;
            }
        }

So I just use it like so:


   existingCompetitor.InjectFrom<nullableinjection>(importedCompetitor);

0 comments:

Post a Comment