Assume some domain and view objects (that have no common base class)
public class DomainA
{
public int MyProperty { get; set; }
}
public class ViewA
{
public int MyProperty { get; set; }
}
public class DomainB
{
public string MyProperty { get; set; }
}
public class ViewB
{
public string MyProperty { get; set; }
}
and a class that can convert from domain to corresponding view
public class ViewFactory
{
public static ViewA Create(DomainA value)
{
return new ViewA() { MyProperty = value.MyProperty };
}
public static ViewB Create(DomainB value)
{
return new ViewB() { MyProperty = value.MyProperty };
}
}
Is it possible to write a method along the lines of
public static List<T> Create<T, U>(IEnumerable<U> values)
{
return new List<T>(from v in values where v != null select Create((U)v));
}
that can convert lists of the "domain" objects to lists of the "view" objects given that there are already methods for converting a single object?
I feel like I'm missing something really stupid and basic about generics asking this question..better to learn the answer than remain clueless though :)