开发者

Layers in ASP.NET MVC

I'm new to A开发者_开发技巧SP.NET and MVC and I just want to ask what is the business logic layers in MVC, and service layers and data access layers?

I'm just wondering what classes in MVC are the layers? Like the controller or the model class?

Please advise.

Many thanks.


The Controller receives an HTTP request, it calls some method on the service layer, which in turn calls the data access layer in order to fetch or manipulate some domain object which is then converted into a view Model and passed to the View. The conversion between the domain model and the actual view model could be done in a separate mapping layer or by the controller (although separating it is preferred as it avoids cluttering the controllers).

So in terms of layers you have the UI layer which consists of controllers, view models and views. Then you could have a business models layer where all domain objects are defined, a data access layer which is responsible for CRUDing those domain objects against a specific data source, and service layer which defines the business operations on the domain objects.


M in MVC do not stand for the ViewModel but for everything that is used to get information into the controller (which adapts it and put it in a view). M is in other words the data source that the controller uses. It can be a webservice, a repository or anything else.

MVC doesn't really care about your layers.

Responsibilities:

  • M: Used to provide data to the controller
  • C: Adapt the data received from M and put it into the view
  • V: Present the data for the user.

Try to honor those responsibilities. Do not add logic into your views. Do not add business logic into your controllers. Keep everything nice and separated. It makes it a lot easier to extend and maintain your application.

ASP.NET MVC

In ASP.NET MVC we have View Models which is used to remove logic from the views. If need to display a message like "n/a" for a property that isn't specified, do it in the viewmodel and not in the view.

The HtmlHelpers was created for the exact same reason: To remove logic from your views.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜