Tony Marston's Blog About software development, PHP and OOP

DTOs are Diabolical

Posted on 24th November 2024 by Tony Marston
Introduction
Counter arguments
Conclusion
References
Comments

Introduction

No sooner have I commented on Back to Basics - Three or Four OOP Pillars? which erroneously states that data abstraction is the fourth pillar of OOP (which it is not) and that data abstraction means using Data Transfer Objects (DTO) (which it does not) when I come across different articles which encourage other programmers to use DTOs. These erroneous articles are:

A DTO is an object that holds state but no behaviour, which is why it has been described as an anemic domain model, an anti-pattern which is described by Martin Fowler as follows:

The fundamental horror of this anti-pattern is that it's so contrary to the basic idea of object-oriented designing; which is to combine data and process together. The anemic domain model is just a procedural style design, exactly the kind of thing that object bigots like me ... have been fighting since our early days in Smalltalk. What's worse, many people think that anemic objects are real objects, and thus completely miss the point of what object-oriented design is all about.

I no not use DTOs in my multi-award winning framework for the simple reason that Encapsulation was described as The act of placing data and the operations that perform on that data in the same class. The notion that I should split this up and put behaviour in one class and data in another class never entered my head. If it had I would have dismissed immediately as a stupid idea.

When I built my RADICORE framework I started by basing it on the 3-Tier Architecture which I had encountered in a previous language. This has a Presentation layer which dealt with HTML, a Data Access layer which dealt with SQL, and a Business/Domain layer which dealt with the business rules. The first thing that I noticed with PHP was that it did not use predefined structs (also known as composite data types, records or data buffers), it used untyped arrays. I noticed straight away that regardless of what data was submitted on an HTML form it appeared in a single $_POST array, and regardless of what data was being read from the database it appeared as another untyped array. Having a logical mind and being a pragmatist I asked myself a simple question: If data in the two outer layers appears as an array is there any reason why I cannot leave it in that array when it passes through the business/domain layer? The answer was "No", so I decided not to waste any time (or keystrokes) in transforming that array into another format so that it could be processed by my domain (Model) objects. I have been using this method for over 20 years, and it has proved to be both simple and effective.

Counter arguments

I shall now step through those articles and argue against every claim that DTOs are a Good Thing ™.

  1. The first article under the microscope is Transform Data into Type-safe DTOs with this PHP Package.

    This PHP Data Model package provides a lightweight, non-invasive way to hydrate type-safe PHP objects recursively. It uses reflection and PHP attributes to hydrate objects and instantiate them based on type hints:

    Whoa! Anybody who says that it is necessary to put data into a type-safe PHP object is not singing from the same hymn sheet as the designers of PHP. Data appears in untyped arrays, and the idea of strict typing is alien to PHP, as described in RFC: Strict and weak parameter type checking where it says PHP's type system was designed from the ground up so that scalars auto-convert depending on the context.

    The idea of wasting time and effort in doing something which is totally unnecessary is also a violation of YAGNI. It is only something that a dogmatist would do. A pragmatist wouldn't touch it with a barge pole.

    The main features of this argument are as follows:

  2. The second article to be questioned is Building Maintainable PHP Applications: Data Transfer Objects:


Conclusion

As soon as I started working with PHP I knew that it was capable of doing everything that I needed. I was particularly impressed with the way it could handle data in flexible arrays instead of static structures. As an avid follower of the KISS principle I just love the way it lets me get the job done with the minimum of effort.

Then along comes a bunch of dunderheads who see to favour the KICK principle instead. They take something which is simple and add unnecessary complexity just to prove how clever they are. These clueless newbies don't realise that what they are doing is proving the exact opposite.

Here endeth the lesson. Don't applaud, just throw money.


References

These are reasons why I consider some ideas on how to do OOP "properly" to be complete rubbish:


counter