Skip to content

Latest commit

 

History

History
192 lines (120 loc) · 5.79 KB

File metadata and controls

192 lines (120 loc) · 5.79 KB

Code Smell 40 - DTOs

Code Smell 40 - DTOs

DTOs are widely used and the 'solve' real problems, do they?

TL;DR: Don't use DTOs

Problems

  • Anemic Object

  • Inconsistent Data

  • Duplicated logic

  • Class Polluting

  • Information Hiding

  • Code repeated among mutators, accessors, serializers, parsers

  • Ripple Effect

  • Data integrity

Solutions

  1. Transfer anemic data on arrays

  2. Use real business objects

  3. If we want to transfer partial objects: use proxies or null objects to break the reference graph.

Refactorings

Refactoring 009 - Protect Public Attributes

Sample Code

Wrong

<?

final class SocialNetworkProfile {

    private $userName;
    private $friends; // friends is a reference to a large collection
    private $feed; // feed references the whole user feed

    public function __construct($userName, $friends, UserFeed $feed) {
        $this->assertUsernameIsValid($userName);
        $this->assertNoFriendDuplicates($friends);
        $this->userName = $userName;
        $this->friends = $friends;
        $this->feed = $feed;
        $this->assertNoFriendOfMyself($friends);
    }
    // Lots of protocol
}

// If you need to transfer to an external system you need
// to duplicate (and maintain) the structure

final class SocialNetworkProfileDTO {

   private $userName; // duplicated to be synchronized
   private $friends; // duplicated to be synchronized
   private $feed; // duplicated to be synchronized
   public function __construct() {
        // Empty constructor without validations
   }

   // No protocol, just serializers
}

// If you need to transfer to an external system you create an anemic DTO
$janesProfileToTransfer = new SocialNetworkProfileDTO();

Right

<?

final class SocialNetworkProfile {

    private $userName;
    private $friends; // friends is a reference to a large collection
    private $feed; // feed references the whole user feed

    public function __construct(
        $userName,
        FriendsCollection $friends, 
        UserFeedBehavior $feed) 
    {
        $this->assertUsernameIsValid($userName);
        $this->assertNoFriendDuplicates($friends);
        $this->userName = $userName;
        $this->friends = $friends;
        $this->feed = $feed;
        $this->assertNoFriendOfMyself($friends);

    }
    // lots of protocol associated with the profile
    // No serialization protocol
    // No behavior or attribute duplication
}

interface FriendsCollectionProtocol { }

final class FriendsCollection implements FriendsCollectionProtocol { }

final class FriendsCollectionProxy implements FriendsCollectionProtocol {
    // proxy protocol
    // travels as a lightweight object and can get contents when requested
}

abstract class UserFeedBehavior { }

final class UserFeed extends UserFeedBehavior { }

final class NullFeed extends UserFeedBehavior {
    // throws an error when requested for behavior
}

// If you need to transfer to an external system you create a valid object
$janesProfileToTransfer = new SocialNetworkProfile(
    'jane', 
    new FriendCollectionProxy(), 
    new NullFeed()
);

Detection

We can use the same anemic object detectors.

We can check for anemic classes with no business object behavior (removing serializes, constructors, mutators etc).

Tags

  • Anemic

Conclusion

DTOs are a tool and an established practice in some languages. We should use them with care and responsibility.

If we need to disassemble our objects in order to send them away from our realms, we need to be extremely cautioned. Since dismembered objects have no integrity considerations.

His author warns us about its actual abuse.

Relations

Code Smell 01 - Anemic Models

Code Smell 13 - Empty Constructors

Code Smell 50 - Object Keys

Code Smell 139 - Business Code in the User Interface

More Info

Martin Fowler on DTOS

Refactoring.guru

Stack Exchange

Credits


The best smells are something that's easy to spot and most of time lead you to really interesting problems. Data classes (classes with all data and no behavior) are good examples of this. You look at them and ask yourself what behavior should be in this class.

Martin Fowler

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code