Consider the code below (which has been simplified). I have a service class that returns a list of specific DTO objects that each implement their own specific interface. In
Have a look at AutoMapper. And I agree with @duffymo, I wouldn't use interfaces with DTO's. AutoMapper is a convention-based object to object mapper that will create and populate your DTO's for you. If nothing else it will save you a lot of typing. I've been through the exercise of writing conversion routines to/from DTO's with associated typos. I wish I had found AutoMapper a bit sooner. In the case of your example (where I've nominally made the "from" object of type Order):
public class Services : IServices
{
public IList GetDTOs()
{
...
Mapper.CreateMap(); // move map creation to startup routine
var dtos = new List();
foreach (c in d)
{
dtos.Add( Mapper.Map(c));
}
return dtos;
}
}
Or using LINQ
public class Services : IServices
{
public IList GetDTOs()
{
...
Mapper.CreateMap(); // move map creation to startup routine
return d.Select(c => Mapper.Map(c)).ToList();
}
}