dimanche 23 avril 2017

Should I create several model types for presentation layer?

Suppose I have entity hierarchy, e. g. House which has a list of Apartment objects and a list of Owner objects (let it be something like hotel). I. e., the simplified structure will look like:

public class House {
    // lazy
    public List<Owner> owners;
    // lazy
    public List<Apartment> aparts;
    public String name;
    public Integer id;
    // some other props
}

public class Apartment {
    // some props
}

public class Owner {
    public String name;
    public int age;
    // tons of other props
}

Now, if I want to pass a JSON representation to presentation layer, I'm creating something like HouseModel with all the properties and converter which would populate all the properties, including apartments and owners. This would be fine if I need to show one house with all the details and stuff though if I later need to create e. g. list of houses in the city it will be waste of time on converting apartments and owners as I will need only names and ids for such a list. Some other case might require only owners to be fetched, another one - apartments etc. Same for other entities and, of course, real life example will be much more complicated.
Does anybody know any good pattern/solution to this problem?

Aucun commentaire:

Enregistrer un commentaire