Three Examples of DTOs in Ruby

mhz
The Startup
Published in
3 min readApr 17, 2020

--

Objects transferring stuff! Like… Maybe… Data?

As our application became more complex, we needed to expand beyond the default MVC model that Rails offers out of the box. Existing software architecture patterns are a great resource that would help in identifying the best strategy for the need. One of such patterns is called the DTO, or Data Transfer Object.

In this short excerpt I will be summarizing what a DTO is, as well as providing 3 examples of the DTO pattern written in Ruby. The reference to these notes are from Martin Fowler’s Patterns of Enterprise Application Architecture.

Data Transfer Object (DTO)

In summary, it is an object whose sole purpose is to haul data. It’s main benefit is the decoupling of data transfer from server, client, and domain logic. This is great if the amount of data transferred is large, hierarchical, or difficult to manage.

A few points to note about DTOs:

  • Should be composed of primitive attributes, and/or other DTOs
  • DTO hierarchy should be simple (if applicable)
  • Is responsible for serialization and de-serialization of itself (If applicable).(JSON, XML, binary, etc)
  • Can be mutable — putting in data gradually
  • Request and Response (to, from server) can use same DTO, or have different…

--

--

mhz
The Startup

Software Engineer. Using this platform to share knowledge on software, and reflections in life.