4

I'm trying to use ValueInjector to flatten a class and to have it also copy across values from Nullable<int>'s to int's.

Eg given the following (contrived) classes:

class CustomerObject
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public OrderObject OrderOne { get; set; }
}

class OrderObject
{
    public int OrderID { get; set; }
    public string OrderName { get; set; }
}

class CustomerDTO
{
    public int? CustomerID { get; set; }
    public string CustomerName { get; set; }
    public int? OrderOneOrderID { get; set; }
    public string OrderOneOrderName { get; set; }
}

I would like to flatten an instance of CustomerObject to a CustomerDTO, with it ignoring the fact that the CustomerID and OrderID's are of different types (one is nullable one isn't).

So I would like to do this:

CustomerObject co = new CustomerObject() { CustomerID = 1, CustomerName = "John Smith" };
co.OrderOne = new OrderObject() { OrderID = 2, OrderName = "test order" };

CustomerDTO customer = new CustomerDTO();
customer.InjectFrom<>(co);

And then have all of the properties populated, specifically:

customer.CustomerID 
customer.OrderOneOrderID 
customer.OrderOneOrderName

I realise I can use FlatLoopValueInjection to flatten out the object, and I'm using this NullableInjection class:

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;
    }
}

Basically I'd like to combine the two. Is this possible?

Omu
  • 69,856
  • 92
  • 277
  • 407
Rocklan
  • 7,888
  • 3
  • 34
  • 49

3 Answers3

9

you could do this by overriding the TypesMatch method:

    public class MyFlatInj : FlatLoopValueInjection
    {
        protected override bool TypesMatch(Type sourceType, Type targetType)
        {
            var snt = Nullable.GetUnderlyingType(sourceType);
            var tnt = Nullable.GetUnderlyingType(targetType);

            return sourceType == targetType
                   || sourceType == tnt
                   || targetType == snt
                   || snt == tnt;
        }
    }

or by grabbing the FlatLoopValueInjection from the source code and edit it as you need (it's about 10 lines)

Omu
  • 69,856
  • 92
  • 277
  • 407
  • Works great CN!! Now my only question is... how did you work that out? – Rocklan Apr 19 '12 at 23:08
  • Ahhh I see, you wrote the thing :) Great tool! I used automapper for my last project, it was driving me nuts, ValueInjector seems a lot simplier and easier to use... so great job :) – Rocklan Apr 19 '12 at 23:40
0

ValueInjecter V3 removes FlatLoopValueInjection. Below is update of Omu's answer.

public class FlatLoopInjectionNullable : Omu.ValueInjecter.Injections.FlatLoopInjection
{
    protected override bool Match(string propName, PropertyInfo unflatProp, PropertyInfo targetFlatProp)
    {
        var snt = Nullable.GetUnderlyingType(unflatProp.PropertyType);
        var tnt = Nullable.GetUnderlyingType(targetFlatProp.PropertyType);

        return propName == unflatProp.Name
            && unflatProp.GetGetMethod() != null
            && (unflatProp.PropertyType == targetFlatProp.PropertyType
                || unflatProp.PropertyType == tnt
                || targetFlatProp.PropertyType == snt
                || (snt != null && snt == tnt));
    }
}

Call it like this:

target.InjectFrom<ValueInjecterNullable>(source);
BillVo
  • 565
  • 1
  • 5
  • 20
-1
// !!! THIS IS FOR LoopInjection not FlatLoopValueInjection !!!
public class NullableInjection : LoopInjection
{
    public NullableInjection() : base() { }

    public NullableInjection(string[] ignoredProps) : base(ignoredProps) { }

    protected override bool MatchTypes(Type source, Type target)
    {
        // This is the most likely scenario test for it first.
        bool result = source == target;
        // if not a type match then lets do more expensive tests.
        if (!result)
        {
            var snt = Nullable.GetUnderlyingType(source);
            var tnt = Nullable.GetUnderlyingType(target);

            // Make sure that underlying types have not reverted to null       
            // this will cause false positives.
            result = ((source == target)
                   || ((tnt != null) && source == tnt)
                   || ((snt != null) && target == snt)
                   || ((tnt != null) && snt == tnt));
        }
        return result;
    }
}
Chewy
  • 1
  • 1
  • I was getting an error on properties that were objects; both the tnt and snt would be null and match. This scenario could lead to an assignment of different object types and cause an exception('System.ArgumentException': Additional information: Object of type 'Namespace.A' cannot be converted to type 'Namespace.B'). – Chewy Feb 04 '16 at 11:47