Should I create interfaces for data transfer objects?

Is it a good idea or a bad idea to create an interface for data transfer objects? Presuming that the object is usually mutable.

Though my example is in Java, it should be applicable to any other language that has similar concepts.

interface DataTransferObject  {
    String getName();
    void setName(String name);
}

class RealDataTransferObject  implements DataTransferObject {

    String name;
    String getName() {
        return name;
    } 
    void setName(String name) {
        this.name = name;
    }
}

Of course, this is a simplified example, in real life there may be more fields.

The general answer is no, because you should never add code without having a specific, concrete reason for it, and there is no general reason for such an interface.

That being said, sometimes there can be a good reason. But in all cases I have seen, these interfaces were partial, covering only one or a few properties shared by multiple classes I wanted to use polymporphically without giving them a common superclass. Typical candidates are an Id property to use in some sort of registry or a Name property to display to the user. But it can be useful in any case where you want some code to handle everything that has an X – just create an XSource interface that contains the getX (and, only if required, the setX) methods.

But a separate interface for every model class, containing all the properties? I can’t imagine a good reason to do that. A bad reason would be a badly designed framework that requires it; Entity EJBs did just that, if I remember correctly. Thankfully they were so bad they never gained much traction and are deprecated since EJB 3.0

Sidenote: please avoid using the term “value object” to describe Java beans with only trivial getters and setters – it conflicts with the more common definition of value object as something with no identity that is usually immutable. A better term would be DTO or model class – though in the latter case note that anemic domain models are considered an antipattern.

6

Interfaces define a contract between the classes implementing the interfaces and their clients. They are used as an abstraction mechanism so that clients can manipulate “stuff having a given behaviour”.

So the general answer to the question “should I create and use this interface?” is: Yes, if you can associate a (a single one) concept being semantically relevant for your clients.

For instance, Comparable is a good interface, because it explains that things can be compared thanks to one of their methods, and as a client I’m interested in dealing with comparable objects (e.g. for sorting them). A contrario, CoolStuff is not a good interface if you admit the cool objects don’t have a specific behaviour (in fact, you can imagine a software in which dealing with cool objects makes sense, because they have a common behaviour such as a beCool method).

In your particular case, I believe your interface is useless. Who will use it, how and when? You cannot create an interface for each of the mutable values. So ask yourself what is the relevant and interesting property behind your methods.

If what you want is dealing with objects having all their mutable values accessible through a couple of methods, have a look at the notion of Java bean and the way you can force your classes to adopt their conventions.

1

Creating interface is ok, but NOT the way your example works. You should remove the setter from the interface, and it’ll be ok:

interface ValueObject {
  String getName();
};

This allows many different implementations of it, like name can be fetched from database… Setter should be in different interface.

As Michael said, don’t add an interface unless you have a specific need for it.

Testing is an example of a good reason. Though I prefer to use real collaborators if they are just “value objects” as you call them, for true unit test isolation you may need to create a fake object for testing, in which case an interface is quite helpful.

3

If you want this object to have some sort of fields validation in future, you should incapsulate them on early stages.

The ‘interface for value object’ is what i’m used to call an accessor.

I experienced different policies concerning accessors need. Some people advocate for when as much as possible, some other prohibit then to reduce the quantity of code to write.

Some rationnals for accessors (or direct value usage) are the following:

  • Accessors allows changing later the way the value is stored
  • Accessors allows adding access log when you want to debug your software (when adding a log call in a single method, you capture every value change)
  • Accessors are more in line with object programming, every variable being encapsulated by methods
  • Accessors reduces code expressivness (more SLOC for the same result)
  • Accessors takes some CPU

I personally advocate to reduce the number of accessors, and to use them when you expect the setter (setName) to become more than a simple affectation later.

This kind of value object is pretty low level. I’d suggest pushing it in one of two directions: (1) make the value object immutable, that is, like a real value, or, (2) elevate the mutability to a higher level domain oriented business function, meaning we should expose interfaces in terms of domain relevant units of functionality.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật