Yes or no: Should models in MVC contain application logic?

后端 未结 6 931
礼貌的吻别
礼貌的吻别 2020-12-23 09:54

Yesterday I had some discussion with one of our developers regarding MVC, more precisely about the role of the model component in MVC.

In my opinion, a model should

6条回答
  •  醉梦人生
    2020-12-23 10:29

    In short I believe the Model should just be data that will be sent to your View. It helps drive the MVC paradigm into other aspects of your application.

    If you are tring not to break the MVC pattern your data should all be returned as a Business Model to your controller and unpacked into your ViewModel. Request the information server side and then send everything. If you need to make JSon requests then that should either be a Rest Service or calls to a Controller. Having these getTags and getArticles makes it very messy ... if your view is now deciding on which to call ... I cant understand why you dont have that information isnt available upfront. Using static methods is the same approach just a different angle.

    I have found it best to have my controller actions call an injected service which does the magic and use the Models within the MVC web application to return the information. This makes things neater and further emphasisis the seperation of concern. Your Controller Actions then become very lean and its clear what they are doing.

    I believe starting by treating the Model as completly dumb might go a long way in sorting some of these architectural problems I am seeing from this code.

提交回复
热议问题