MVC: I need to understand the Model

I've been working with the MVC pattern for a while now, but I honestly don't feel like I truly understand how to work with and apply the "Model" ... I mean, one could easily get away with using only the Controller and View and be just fine.

I understand the concept of the Model, but I just don't feel comfortable applying it within the pattern... I use the MVC pattern within .NET and also Wheels for ColdFusion.

"the Model represents the information (the data) of the application and the business rules used to manipulate the data" - yes, I get that... but I just don't really understand how to apply that. It's easier to route calls to the Controller and have the Controller call the database, organize the data and then make it available to the View. I hope someone understands where my confusion resides...

I appreciate your help in advance!


Look at it like this. When your client requests a page this is what happens (massively trimmed):

  • He ends up at your controller

  • The controller gets the necessary data from your model

  • The controller then passes the data to the view which will create your HTML

  • The controller sends the HTML back to the client

  • So client -> controller -> model -> controller -> view -> controller -> client

    So what is the model ? It is everything that is required to get the data required for you view!

  • It is services

  • It is data access

  • It is queries

  • It is object mapping

  • It is critical 'throw exception' style validation

  • Your controller should not be writing your queries if you are sticking to the pattern. Your controller should be getting the correct data required to render the correct view .

    It is acceptable for your controller to do a few other things such as validating posted data or some if/else logic but not querying data - merely calling services (in your model area) to get the data required for your view.


    I suppose it's just what you decide to call the different bits in your application. Whichever class you use to pass information from the Controller to the View can be seen as/called "The Model".

    Typically we call Model our entity classes, and we call View Model the "helper" classes, for lack of a better word, we use when a "pure" entity (ie, one that will be stored in the database) doesn't suffice to display all the information we need in a View, but it is all mostly a naming thing.

    Your model classes shouldn't have any functions; ideally a model class will have only properties. You should see model classes as data containers, information transporters. Other than that they are (mainly) "dumb" objects:

    // This would be a model class representing a User
    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }
    

    How do you actually pass information (whatever that might mean in your context) form your controller to your View and vice versa? Well then, that's your model. :)


    Model is the code representation of your base Objects. While some less data-intensive systems may be light on the model end of MVC, I am sure you will always find an applicable use.

    Let's take a contrived (but realistic) example to the usefulness of a model:

    Say I am making a Blog. My Blog has Post objects. Now, Posts are used in and around the site, and are added by many users in the system. Our system was coded for people to enter HTML into their posts, but low and behold, people are starting to add pasted text. This text uses "n" as the newline character.

    With a model, this is a relatively simple fix. We simply make a getter that overrides postText:

    public function get postText() {
        return this.postText.replace("n", "<br />");
    }
    

    Suddenly, we can affect behavior across the entire site with a few lines of simple code. Without the implementation of a model, we would need to find and add similar functionality where ever postText is used.

    The Model in MVC is all about encapsulation and flexibility of a codebase as it evolves over time. The more you work with it and think about it in this manner, the more you will discover other cases that would have been a nightmare otherwise.

    --EDIT (you have added to your question above):

    Let us take this same example and use your Controller calling the database. We have 9 Controller classes for vaious pages/systems that use Post objects. It is decided that our Post table needs to now have a delete_fl . We no longer want to load posts with delete_fl = 1 .

    With our Post model properly implemented, we simply edit the loadPosts() method, instead of hunting down all the cases across the site.

    An important realization is that in any major system, the Model is more of a collection of files than a single monolith. Typically you will have a Model file for each of your database tables. User, Post, etc.

    链接地址: http://www.djcxy.com/p/52924.html

    上一篇: 如何使用Maven类路径运行Java主类?

    下一篇: MVC:我需要了解模型