Programming

Should services always return DTOs or can they also return domain models closed

25 September 2026 · 9 min read

Should services always return DTOs or can they also return domain models closed

Building robust and maintainable applications often involves grappling with architectural decisions, and one common dilemma revolves around data transfer: should services consistently return Data Transfer Objects (DTOs), or can they sometimes return domain models directly? This question lies at the heart of efficient data handling and can significantly impact the overall architecture of your software. Choosing the right approach requires careful consideration of various factors, including system complexity, security concerns, and performance requirements.

DTOs vs. Domain Models: Understanding the Core Difference

Domain models represent your core business logic and data structure. They encapsulate the rules and relationships within your application’s domain. DTOs, on the other hand, are specifically designed for data transfer between layers or systems. They are streamlined representations of data, often omitting business logic and focusing solely on the information needed for a specific operation.

The key distinction is that domain models are concerned with behavior, while DTOs are concerned with data. This separation allows for greater flexibility and decoupling between different parts of your application.

For instance, imagine an e-commerce platform. A domain model for a “Product” might include methods for calculating discounts or managing inventory. A DTO for representing a product in a shopping cart, however, would only need fields like “product ID,” “name,” and “price.”

Benefits of Using DTOs

DTOs offer several advantages, especially in complex systems. They promote loose coupling by preventing direct exposure of domain models to external layers. This decoupling simplifies maintenance and evolution of the application. DTOs also enhance security by limiting data access to only what is necessary, reducing the risk of unintended data exposure or modification.

Furthermore, DTOs improve performance by reducing payload size, especially when transferring data over a network. By sending only required fields, DTOs minimize bandwidth consumption and serialization/deserialization overhead.

  • Enhanced Security
  • Improved Performance

When Domain Models Might Be Suitable

In simpler applications with minimal layers and less stringent security requirements, returning domain models directly might be acceptable. This approach reduces the overhead of creating and mapping DTOs, simplifying development. However, even in such cases, careful consideration should be given to potential long-term implications as the application grows and evolves.

If you’re working with a small, self-contained application where performance and security are less critical, directly using domain models can expedite development. However, this approach can become problematic as the application scales or if security concerns arise.

Consider the potential for future expansion. If you anticipate future growth or integration with other systems, using DTOs from the outset can save you from significant refactoring later.

Best Practices for Choosing the Right Approach

Choosing between DTOs and domain models requires evaluating your specific needs and priorities. Factors like system complexity, security requirements, performance goals, and team expertise should influence your decision.

Here are some steps to help guide your choice:

  1. Analyze your application’s architecture and complexity.
  2. Evaluate your security requirements.
  3. Consider performance implications.
  4. Assess your team’s expertise and preferences.

By carefully considering these factors, you can make an informed decision that best suits your project’s specific needs. A well-defined data transfer strategy is essential for building a scalable, maintainable, and secure application.

Real-World Example

Consider a banking application. Transferring sensitive customer data directly from the domain model could pose significant security risks. Using a DTO allows you to selectively expose only necessary information, such as account balance and transaction history, while protecting sensitive data like social security numbers and passwords.

This example illustrates the importance of aligning your data transfer strategy with the security requirements of your application.

Another scenario is a distributed system where multiple microservices communicate with each other. In such cases, DTOs are almost essential to ensure efficient data transfer and loose coupling between services. They minimize the impact of changes in one service on others, promoting greater autonomy and flexibility.

Infographic Placeholder: Visual comparison of DTOs and Domain Models.

FAQ

Q: When are DTOs most beneficial?

A: DTOs are most advantageous in complex, distributed systems with high security requirements and a need for optimized data transfer.

  • Decoupling
  • Data Integrity

Successfully navigating the DTO vs. domain model decision empowers developers to build robust, adaptable applications. By understanding the core distinctions and considering your project’s specific needs, you can choose the approach that best aligns with your long-term goals. Prioritizing a well-defined data transfer strategy sets the stage for a scalable and maintainable application architecture. Explore further resources on DTOs, domain models, and data transfer strategies to deepen your understanding. Remember to tailor your approach to the unique demands of your project for optimal results. Dive deeper into these concepts by exploring the provided resources and experimenting with different approaches in your own projects. Consider the long-term implications of your choice, and don’t hesitate to adapt your strategy as your application evolves. Learn more about data transfer objects on this insightful page.

Question & Answer :

I'm (re)designing large-scale application, we use multi-layer architecture based on DDD.

We have MVC with data layer (implementation of repositories), domain layer (definition of domain model and interfaces - repositories, services, unit of work), service layer (implementation of services). So far, we use domain models (mostly entities) across all layers, and we use DTOs only as view models (in controller, service returns domain model(s) and controller creates view model, which is passed to the view).

I’v read countless articles about using, not using, mapping and passing DTOs. I understand that there’s no any definitive answer, but I’m not sure if it’s ok or not returning domain models from services to controllers. If I return domain model, it’s still never passed to the view, since controller always creates view-specific view model - in this case, it seem legit. On the other hand, it doesn’t feel right when domain model leaves business layer (service layer). Sometimes service needs to return data object that wasn’t defined in the domain and then we either have to add new object to the domain that isn’t mapped, or create POCO object (this is ugly, since some services return domain models, some effectively return DTOs).

The question is - if we strictly use view models, is it ok to return domain models all the way to controllers, or should we always use DTOs for communication with service layer? If so, is it ok to adjust domain models based on what services need? (Frankly I don’t think so, since services should consume what domain has.) If we should strictly stick to DTOs, should they be defined in service layer? (I think so.) Sometimes it’s clear that we should use DTOs (e.g., when service performs lot of business logic and creates new objects), sometimes it’s clear that we should use just domain models (e.g., when Membership service returns anemic User(s) - it seems it wouldn’t make much sense to create DTO that is the same as domain model) - but I prefer consistency and good practices.

Article Domain vs DTO vs ViewModel - How and When to use them? (and also some other articles) is very similar to my problem, but it doesn’t answer this question(s). Article Should I implement DTOs in repository pattern with EF? is also similar, but it doesn’t deal with DDD.

Disclaimer: I don’t intend to use any design pattern only because it exists and is fancy, on the other hand, I’d like to use good design patterns and practices also because it helps designing the application as a whole, helps with separation of concerns, even though using particular pattern isn’t “necessary”, at least at the moment.

it doesn’t feel right when domain model leaves business layer (service layer)

Makes you feel like you are pulling the guts out, right? According to Martin Fowler: the Service Layer defines the application’s boundary; it encapsulates the domain. In other words, it protects the domain.

Sometimes service needs to return data object that wasn’t defined in the domain

Can you provide an example of this data object?

If we should strictly stick to DTOs, should they be defined in service layer?

Yes, because the response is part of your service layer. If it is defined “somewhere else” then the service layer needs to reference that “somewhere else”, adding a new layer to your lasagna.

is it ok to return domain models all the way to controllers, or should we always use DTOs for communication with service layer?

A DTO is a response/request object, it makes sense if you use it for communication. If you use domain models in your presentation layer (MVC-Controllers/View, WebForms, ConsoleApp), then the presentation layer is tightly coupled to your domain, any changes in the domain requires you to change your controllers.

it seems it wouldn’t make much sense to create DTO that is the same as domain model)

This is one of the disadvantages of DTO to new eyes. Right now, you are thinking duplication of code, but as your project expands, then it would make much more sense, especially in a team environment where different teams are assigned to different layers. See WET principle.

DTO might add additional complexity to your application, but so are your layers. DTO is an expensive feature of your system, they don’t come free.

Why use a DTO

This article provides both advantage and disadvantage of using a DTO: http://guntherpopp.blogspot.com/2010/09/to-dto-or-not-to-dto.html

Summary as follows:

When to Use

  • For large projects.
  • Strategic, mission critical application.
  • Large teams (more than 5).
  • Developers are distributed geographically.
  • The domain and presentation are different.
  • Reduce overhead data exchanges (the original purpose of DTO).

When not to Use

  • Small to mid-size project (5 members max).
  • Expected project lifetime is 2 years or less.
  • No separate team for GUI, backend, etc.

Arguments Against DTO

Arguments For DTO

  • Without DTO, the presentation and the domain is tightly coupled. (This is ok for small projects.)
  • Interface/API stability
  • May provide optimization for the presentation layer by returning a DTO containing only those attributes that are absolutely required. Using linq-projection, you don’t have to pull an entire entity.
  • To reduce development cost, use code-generating tools