Veil Objects to Replace DTOs
Here is a new idea I discovered just a few days ago while working with Codexia, a Ruby web app. I had to fetch data rows from PostgreSQL and return objects to the client. It’s always been a problem for me, how to do that without turning objects into DTOs. Here is the solution I found and gave a name: Veil Objects.
Let’s say I fetch the list of projects from PostgreSQL:
It would be great to make the method
fetch() return an array
of objects, not an array of Hashes. So my class
Project looks like this:
It’s perfectly designed for single-project manipulations:
Two SQL requests here is not a big deal. However, if I convert the list of Hashes to Projects like this, I will have serious performance problems:
This is what will kill me, performance-wise:
This code will generate too many redundant SQL requests. We will do round-trips
to PostgreSQL to fetch the data we had a few milliseconds ago, while
we were doing
SELECT * FROM project.
The easiest and the most obvious solution, which many of you might suggest,
is to encapsulate the retrieved Hash into the
Project object. In other
Project into a DTO,
a holder of data. Well, in this case we might
not even need an object, but can instead return the Hash with the data. But
this is not how we want our object-oriented software to be designed. We want
to deal with objects, not data structures. And, at the same time, we don’t want objects to
be stupid enough to go back to the database for the same data we had
a second ago. Here is the solution I’m proposing:
Veil object from veils gem
is a decorator of
Project. It behaves like
Project, but some of the methods on it are redefined:
When they are called, the calls won’t reach the encapsulated
Instead, the data stored in the
Veil will be returned.
It is called a “veil” because it acts like one: the preset data is
returned only until some other method is called, which was not preset.
If this happens, the veil is pierced and the
Veil object becomes fully
transparent, sending all method calls through.
Thus the efficiency of DTO is combined with the elegance of OOP.
I’m using these new veil objects in yegor256/codexia, so you can see how they work.
P.S. I also create an
Unpiercable class, which acts exactly like a
but can never be pierced. It is very useful, when you don’t expect any
data-modifying interactions to happen with the object and just want some
of its methods to be pre-calculated.
P.P.S. This is the implementation for Kotlin.