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?