jax
jax

Reputation: 747

Should one place a list of models inside another model?

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?

Upvotes: 0

Views: 395

Answers (1)

Elia Sulimanov
Elia Sulimanov

Reputation: 52

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.

Upvotes: 1

Related Questions