I have a question about MVC. Particularly about models. Suppose that I have a category table in my database. Now I would like to get results both for a single category for detailed view and multiple categories for a listing. Also I may need to query a number of categories for different purposes.
Now the question is; Does it make more sense to have two separate models. Like category model for operations on a single category and categories model operations on multiple categories.
My thinking is that when I am using category
model I don't need additional details for multiple categories. So separating these makes sense to me. But I am not sure.
Any ideas?
It depends, do you need to save different data for a single category and for multiple categories?
If so, your proposal makes sense as otherwise you would have redundant fields in your model. I would advise making a clear distinction between both models (so not
Category
andCategories
, but for exampleSingleCategory
andMultipleCategories
).If not, I would suggest having one model for a Category, but with different operations defined for single and multiple category operations. I assume this is your situation.
In the latter case, you can make use of an abstract super class
Category
and then define two children: one that contains operations for single categories and one that contains operations for multiple categories.