Skip to main content

Multiple models vs single model


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?


Source: Tips4all

Comments

  1. 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 and Categories, but for example SingleCategory and MultipleCategories).

    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.

    ReplyDelete
  2. The thing is that your model should support handling single and multiple record queries.

    So my advice is to use one model and develop your methods to retrieve the exact data you need.

    Having two models for a single data source only complicates stuff...

    ReplyDelete
  3. I can't see any reason for using multiple models for the same collection of data.

    In MVC, model represents collection of data - it can be single or multiple items. If specific model represents only single item, it's still the part of collection of data.

    Why are you wondering about using two separate models?

    ReplyDelete
  4. What we did in our own MVC and ORM is that we created a wrapper for operations on multiple model instances. Which is a ResultSet. The resultset then is able to do operations on for instance an array of Category models.

    For reference: https://github.com/Tuxion/tuxion.cms

    ReplyDelete
  5. It just depends on YOU!

    You are the programmer whatever suits you is what goes.

    However to add my reasoning:

    A single model class would be better in terms of Readability and Maintainability!

    e.g.

    class get_fruits
    {

    function all_fruit(){}

    function one_fruit(){}
    }


    This would be very easy for another programmer reading the code to understand

    e.g.

    $get = new get_fruit();
    $europeanfruits = $get->all_fruits("European");
    $apple = $get->one_fruit ("Apple");


    Hope this helps!

    Remember there is no right or wrong solution just aslong as it works for you!

    ReplyDelete
  6. In my opinion lists, arrays, collections etc. which are provided by the language you are using are valid choices for collections of models, and you should not be creating an additional model that simply wraps this collection as it really achieves nothing at all. You might consider wrapping the collection in a model if you want to associate some interface with the collection.

    If you specifically need to store additional information about the collection then you should wrap it in another model.

    I also disagree with the idea that you shouldn't have multiple models because the data comes from the same data source. In fact I believe you shouldn't be putting your database logic in the model itself, but using a separate service that returns business objects. This level of encapsulation allows you to manipulate the high level models (or business objects) on the application level while decoupling the data access logic from these objects. If you then need to go and swap out the database with something else, you need only replace the data access logic and the interface that creates the models.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex