views:

71

answers:

2

I see DTO types being created within and passed between types in the domain model. Is this good practise?

I always thought DTOs were to be used principally at context boundaries (i.e. at the edge of the object graph) to decouple context implementations (e.g. at the domain / ui boundary).

+1  A: 

Creating a DTO hierarchy that parallels your domain model, just for the sake of layering purity, seems like an anti-pattern to me. I'd argue against it every time.

EJB 1.0 encouraged using DTOs this way, because passing entity EJBs that were chatty was inefficient. People would load the data into DTOs to avoid network traffic. I think it's unnecessary now.

duffymo
+1  A: 

Your question is sort of subjective, but that's ok. As with most "hard and fast rules", there really are no hard and fast rules. There are only guidelines. There is always an exception, or some special case where the best course of action is to do something against best practices (like using a goto statement to instantly break out of multiple nested loops).

That being said, no, passing around DTO types withing your domain model is not a good practice. DTO stands for data transfer object, the transfer typically meaning transport across some boundary. If you're staying inside your domain model, you shouldn't be converting to DTO types and then back to domain types.

Samuel Meacham
Thanks. Do you mean "subjective" in your first sentence? I have to agree with you though - creating DTOs inside the domain model limits flexibility due to DTOs lacking the ability to encapsulate functionality.
Ben Aston
Ha! I certainly did mean "subjective", thanks for catching that (edited).
Samuel Meacham