21

I'd like to use DTO's in my view models in lieu of my domain objects, however I'm having a hard time justifying the maintenance overhead of having to maintain two sets of properties for each domain object.

I was wondering if anyone has implemented or knows of a pattern where the properties of a domain object are separated from the object's actions without having to maintain two sets of properties.

One thought I had would be to have my domain object be only properties and attaching the actions as a subclass:

public class Person{
    private String firstName;
    private String lastName;

    public String getFirstName(){
        return this.firstName;
    }

    public String setFirstName(string firstName){
        this.firstName = firstName;
    }

    ...
}

public class PersonActions extends Person{
    public void save(){
        ...
    }

    public Person get(){

    }
}

This way still feels a bit kludgy as I'd have to pass around a PersonAction class if I wanted a full representation of the domain object.

5
  • Are you using some sort of ORM, or other database abstraction tool, especially one that supports source code generation?
    – Lukas Eder
    Sep 13, 2011 at 14:45
  • Have you heard something about DAO or active record patterns?
    – maks
    Sep 13, 2011 at 14:51
  • It is not a good idea to subclass as regularly if your domain is not anemic it will be rich domain objects and ur DTOs will be flatter and more presentation oriented, so the structure and behavior of them should vary, that is why you need to maintain two separate models with a mapping layer Sep 13, 2011 at 15:18
  • @Lukas Eder I am using hibernate. The problem that I have is that I prefer a rich domain model to an anemic one so my domain objects that hibernate populates have both fields and actions. Sep 13, 2011 at 16:10
  • Your idea is not half bad! If there is one clear dressed-down version of the entity that suits your needs (rather than requiring several different view models for different purposes), I would split into subclasses as follows: PersonDto (base class, no methods, no sensitive properties like auto-increment id), Person (inherits base class, adds auto-increment id, adds methods). Additional abstract layers can be inserted in between if desired. (Note that the ORM would work with one of the subclasses, as it needs at least the id.)
    – Timo
    Aug 11, 2017 at 9:52

4 Answers 4

3

You could use an interface exposing only your object's data, without any domain methods. You'd still need to maintain two classes, but that would be a lot easier since most of the changes could be refactored by your IDE (Eclipse for example). Here's an example:

public interface PersonView {
    String getFirstName();
    String setFirstName();
}

public void Person implements PersonView {
    private String firstName;

    @Override // This annotation guarantees the interface is correct 
    public String getFirstName() {
        return firstName;
    }

    ...domain methods...
}

Is not a perfect solution, but it's a pretty clean one.

As for the problem itself, I for one don't really mind exposing the whole object to the view layer. IMHO, I don't think hiding some methods is worth the overhead. The team should have the discipline to use the objects wisely, but that's just my opinion.

1
  • I like the expressiveness of this approach. I'll have to try it out in some actual working code to really see how it'll work in the real world. Honestly I didn't have much of a problem exposing a rich object in the view, however the biggest problems arose when trying to convert a fully rich domain object to json. Sep 14, 2011 at 1:47
2

Here is another idea:

Use the domain object and only annotate the methods you want exposed. You can make it so they require explicit exposition by Qualifying access type as NONE that way you don't have accidental data exposure

Example:

@Entity // hibernate
@XmlAccessorType(XmlAccessType.NONE) // for DTO
@XmlRootElement
public class Survey {
    @Column(name = "Title") // hibernate 
    @Basic // hibernate
    @XmlElement // for use as DTO
    private String title;

    @XmlElement(name = "report")
    public SurveyReport getSurveyReport() {
          // ... do some stuff here
    }
}

By using this approach you get the best of both worlds. Additionally, you can expose 'meta' information with methods and then annotate the method (available because of XmlAccessType.NONE).

The biggest drawbacks I see:

  1. Annotation explosion resulting in difficult to maintain code

  2. Monolithic object with many methods

    • Which may be great for view consumption but not really so appropriate for internal code
    • Leading to API confusion
    • see #1
  3. At the end of the day you may not be able to represent the view you want without combining information from multiple domain objects; leaving you right back to needing an explicit DTO anyways

1

Just make your model (Person) a property of your controller (PersonActions):

public class PersonActions {

    private Person person;

    public PersonActions() {
        person = new Person(); // Or get existing one from DAO in case of edit.
    }

    public void save() {
        somePersonDAO.save(person);
    }

    public Person getPerson() {
        return person;
    }

}

Based on your question history I understand that you're using Struts. In that case, it's good to know that JSP EL supports nested object properties, something like this to retrieve the values:

${personActions.person.firstName}
${personActions.person.lastName}

There's no need to flatten them by duplicating the properties in the controller.

3
  • I see where you are going with this, and its certainly an approach I could take. The only thing with this approach is that I personally feel that it gives the controller too much control. It definitely underscores the disadvantage of doing things the way I pointed out in my question. Sep 14, 2011 at 1:43
  • Too much control? I guess there's for you some ambiguity in the term "Controller" :) The other answer essentially tight-couples the model with the controller which makes it unportable.
    – BalusC
    Sep 14, 2011 at 2:44
  • By "controller" i'm speaking in context of MVC. To me a MVC controller should just coordinate calling methods from a service layer to get a "unit of work" done. Typically, i'd have one person class that combines both the actions class and the person class (IE Rich domain) so I wouldn't mind tightly coupling the actions of a domain object with its properties. Sep 14, 2011 at 3:13
0

One approach is to have a property in your View Model which is your DTO object.

Another approach is to duplicate the properties, but use something similar to AutoMapper to map between the objects.

In our latest project we have the DTO object as a property, but we also expose the individual properties in the View Model, which reference the properties in the DTO object.

1
  • Shiraz, do you have any code snippets that you can add to your answer that displays these techniques in action? Sep 13, 2011 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.