22
votes

I have an MVC2 n-tier application (DAL, Domain, Service, MVC web) using a DDD approach (Domain Driven Design), having a Domain Model with repositories. My service layer uses a Request/Response pattern, in which the Request and Response objects contain DTO's (Data Transfer Objects) to marshal data from one layer to the next, and the mapping is done via help from AutoMapper. My question is this: what shape should a DTO typically take? Can it have nested/complex DTO's as well or should it strictly be a flat projection? Or possibly a mixture of both? Also, what are the main reasons for having a flat DTO vs a more complex/nested DTO?

For instance, suppose I had a domain such as the following:

public class Employee
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Company Company { get; set; }
}
public class Company
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

There are three different ways I've thought of modeling the Response object.

Option 1 - the DRYest option:

public class GetEmployeeResponse
{
    public class EmployeeDTO { get; set; } // contains a CompanyDTO property
}

From the research I've done, it would be inappropriate for a DTO to take a similar shape as the domain object(s) as demonstrated above.

Option 2 - a flattened projection of the domain (anti-DRY):

public class GetEmployeeResponse
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string CompanyName { get; set; }
    public string CompanyAddress { get; set; }
    public string CompanyCity { get; set; }
    public string CompanyState { get; set; }
}

This is more simple, like a DTO apparently should be, but ultimately makes for more DTOs.

Option 3 - a mixture of both:

public class GetEmployeeResponse
{
    public EmployeeDTO Employee { get; set; }
    public CompanyDTO Company { get; set; }
}

This allows for the code to be a little bit more dry, reusable and manageable, and doesn't expose my domain structure to the end user. The other main benefit is that other responses, like GetCompanyResponse could simply return CompanyDTO, without having to make a copy of all those properties, similar to option 2. What do you think? Which option of these (if any) have you taken and/or have worked for you? If these Request/Responses later get exposed as WCF service methods, does your answer change?

1
Why do you build n-tier MVC application in the first place? I am not saying that it is wrong. Just being curious what advantage you get by putting services between your domain model and web tierSzymon Pobiega
I just want to respond to a specific comment you made: "make a copy of all those properties". Once your system reaches a certain complexity threshold, it may be better to have a dedicated read model that is denormalized at the DB level (either by views, or at your ORM configuration). When I started doing this it allowed me to build much more complex domain models because I didn't have to worry about the expense of hydrating them for the query side of things. I mean, why hydrate multiple models if you're just going to denormalize them? Let the DB do that. It's what it's good at anyway.Ryan
@Szymon There are LOTS of advantages of having a service tier. For me the biggest advantage is I can put all the security in one layer and not let it leak into my controllers.Ryan
@Szymon In addition to @Ryan's comments, my service layer API will eventually be exposed as WCF services for partners to use and develop from. My web app will simply become another client using those web services.tbehunin
@tbehunin How do you know that your partners will find API you designed for usage in Web interface usefull? In my opinion the chance is very small. I would rather directly wired UI with the domain and built service API focused solely on automated usage by external systemsSzymon Pobiega

1 Answers

15
votes

My personal preference would be to try and keep it flat as possible with only the required data being transfered. having said that I have used deeply nested DTO in the past because it made sense at the time and fitted the requirements. so I guess it comes down to "it depends". At the end of the day go with what makes sense for the application at hand. No point trying to shoe horn data into a DTO convention that doesn't fit what you are tying to achieve.