0

In a Java/Spring MVC/Hibernate application I have model objects that contain references to other model objects where the database has foreign keys. So for example, I might have:

@Entity
@Table(name="branches")
public class Branch {
    @Id
    @GeneratedValue
    @Column
    private int id;

    @Column
    private String name;

    // ...
}
@Entity
@Table(name="clients")
public class Client {
    @Id
    @GeneratedValue
    @Column
    private int id;

    @Column
    private String name;

    @ManyToOne
    @JoinColumn(name="branch_id")
    private Branch branch;
    // ...
}

Spring MVC can't easily handle binding request parameters to an object that has references that would need to be pulled in from the database, eg if a form produces fields:

name=John Smith
branch=43

Spring MVC has no easy way of converting that 43 to a filled-out Branch object. To get around this, I create a new class that contains the data required by forms that handle each type (typically this ends up being all the fields from the original class, but sometimes one or two fields that are only used internally or aren't edited by forms get missed out) except that references to other model objects are translated into keys that can be looked up manually. I then add methods to convert to or from the model class. For example:

public class ClientWithKeys {
    private int id;
    private String name;
    private int branch;
    // ...
    public Client toClient (IntFunction<Branch> branchSource) { ... }
    public static ClientWithKeys fromClient (Client c) { ... }
 }

Controller code is then simply:

 @RequestMethod("/whatever")
 public ModelAndView frobnicateClient (@Valid ClientWithKeys clientWithKeys) {
     Client client = clientWithKeys.toClient (clientRepository::getClientById);
     ...
 }

I've been calling these objects Data Transfer Objects, but my attention was recently drawn to the fact that the canonical reference for that name describes a different purpose for the pattern than the purpose for which I've been using these objects. Is actual usage of the name broad enough to cover what I'm doing, or is there a better name I could be using to describe this?

2
  • I think you may be overthinking this a bit. It's just a class. If you want to give it a special name, call it a "Jules class." If you really want angels dancing on the head of a pin, you could squint and call it a variation on the "Adapter Pattern," I suppose. – Robert Harvey Jul 3 '17 at 15:53
  • @RobertHarvey - Maybe. I was just thinking there should be a way to describe such classes (of which I typically end up with quite a few) in the documentation, so everyone understands what they are for. But then there are only 2 developers on this project, so I guess it's not critical. :) – Jules Jul 3 '17 at 16:11
2

The term I think would be most applicable would be view model. It is a model specifically for the view separate from the domain model.

1
  • Yes, that sounds reasonable. Unless a better suggestion comes along soon I'll accept this one. – Jules Jul 4 '17 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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