MVC - is it just a 3 tier model?

不问归期 提交于 2020-02-23 13:10:41

问题


Just began researching mvc, and am not sure I grasp it yet. From what I gather it seems like an implementation of a 3 tier solution ie Model corresponds to DAL, Controller to business logic layer, and View as Presentation layer.

Am I way off base here?


回答1:


I caution against treating the Model as simply a data access layer. That's oversimplifying, and it results in you putting too much code into the Controller Layer. It's better if you put more of that code in the Model, and make database persistence only a part of the Model's internal code. I like to think of MVC like this:

  • Controller: handle input, determine which Model and which View to instantiate
  • View: presentation of application data
  • Model: all other logic for the application, including but not limited to DAL

This is basically the Page Controller pattern.

Another way to think about it is this: suppose you had to port your web app to another platform, such as a command-line app or a desktop GUI app. What parts of the application logic should you reuse? The Controller and View would change as you port your app to another platform, because the implementation of both input and output would need to change. The code that doesn't need to change should be implemented in your Model.

If you've done the separation of concerns right, then the Model, View, and Controller would be minimally coupled, and you could change the implementation of one without affecting the others too much. If you change the Model and you find yourself rewriting a lot of code in the Controller or the View, you probably haven't separated these layers adequately. And vice versa.

Read about Martin Fowler's Anemic Domain Model antipattern or Domain Driven Design Quickly to get some other perspectives.

Also see my blog from 2008 that I wrote in response to people decrying the Active Record pattern. It got some good comments and discussion.




回答2:


Sort of. It looks like this:

The pattern most commonly used today is:

Database -> DAL -> BLL -> Controller -> View Model -> UI

Where

DAL == Data Access Layer (aka ORM, Object-Relational mapper)
BLL == Business Logic Layer

Note that you don't always need every layer. The BLL and View Model, for example, can be optional if the app is small enough.

You should check out the NerdDinner tutorial. It describes all of these concepts in a single reference.




回答3:


Some great explanations here if you're new to MVC:

Does anyone beside me just NOT get ASP.NET MVC?




回答4:


A short note, you are correct when you say the Controller can (doesn't have to) be the business layer, and the view is the presentation layer.

However the model are the objects (depending on implementation) that contain the data, whereas a data layer is a layer that retrieves/manipulates data.



来源:https://stackoverflow.com/questions/1753716/mvc-is-it-just-a-3-tier-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!