0

Should one place a container structure in a model?

For example,

AModel { 

   List<BModel> listB; 
} 

In terms of OO this makes sense. But when working with models and databases will I run into problems as AModel and BModel have their own tables? For instance, when I get AModel from a database do I just leave listB empty, or in order to completely retrieve the object do I also get all of BModel objects that are associated with AModel? However, sometimes I might just want to get AModel and not the list of BModels referencing AModel. Right now, I am considering deleting the list from AModel. This would then better reflect the database schema in which AModel has a 1-to-many relationship with BModel. I feel that it would be a more elegant approach. What do you think?

jax
  • 728
  • 1
  • 11
  • 31
  • 1
    Are you using an ORM such as JPA? If so, those support lazy-loading of linked entities (and it can look just like a nested collection field). – Thilo Jan 13 '19 at 09:57
  • @Thilo Not at the moment. My design is not overly complex and I was thinking I could just manage without one. How did they deal with this before ORM? – jax Jan 13 '19 at 10:00

1 Answers1

1

It is possible, but it's bad practise as it makes the code and reading flow impossible to understand for the programmer and as soon as you have your first logical error it will be impossible to track it down. For more helpful answer, please provide code/ more specific question.

  • I don't think it makes it harder to read. In fact, in terms of OOP, this makes sense to encapsulate the list. However, I feel this is not elegant way to approach it from an MVC approach, at least how I was thinking about it originally. I research some more and found that this is actually pretty common to have a list like this. However, there are performance issues that need to be considered. But I don't those are any I will need to deal with right now. Thanks for replying. – jax Jan 13 '19 at 10:24