ASP .NET MVC架构如何适应传统的多层架构

时间:2021-01-13 01:49:03

Moving from the traditional way of architecting web applications with a Business Layer, Service Layer, Data Access Layer and a Presentation Layer to the MVC design pattern, I find it difficult to understand how it fits in the old model.

从传统的Web应用程序架构方法(包括业务层,服务层,数据访问层和表示层)转变为MVC设计模式,我发现很难理解它如何适应旧模型。

It seems to be that the MVC model itself already has done allot of the separation of concerns that is needed and used to be achieved via a layered architecture. Can someone shed some light on this subject please?

似乎MVC模型本身已经完成了所需的关注分离,这些问题需要通过分层架构来实现。有人可以对这个问题有所了解吗?

As a reference, below is how I understand it, please share your view on this

作为参考,以下是我的理解,请分享您对此的看法

MVC Views and Controllers along with View Models -are- Presentation Layer

MVC视图和控制器以及视图模型 - 表示层

MVC Models - could be - Data Access Layer or Business Layer or even Service Layer

MVC模型 - 可以是 - 数据访问层或业务层甚至服务层

4 个解决方案

#1


36  

I see the Asp.Net MVC part only as the view (or presentation) part of the whole application.

我认为Asp.Net MVC部分仅作为整个应用程序的视图(或演示)部分。

I struggled too with the problem how to structure the app in a proper way.
Following the Onion Architecture I heard about here (and especially the image found here), my solution looks this way:

我也在努力解决如何以适当的方式构建应用程序的问题。按照我在这里听到的洋葱架构(特别是这里找到的图像),我的解决方案看起来像这样:

  • Project.Core
    Business logic/services implementations, entities, interfaces that must be implemented by the other projects (i.e. "IRepository", "IAuthenticationService",...)
  • Project.Core业务逻辑/服务实现,实体,必须由其他项目实现的接口(即“IRepository”,“IAuthenticationService”,......)
  • Project.Data
    DB connection - in my case NHibernate repositories and entity-mappings go here. Implements data-interfaces of Project.Core
  • Project.Data数据库连接 - 在我看来,NHibernate存储库和实体映射都在这里。实现Project.Core的数据接口
  • Project.UI.Web
    The Asp.Net MVC ("presentation") project - it wires the whole app together.
    Has implementations for Interfaces in Project.Core and wires them (and those from Project.Data) up with some DI framework like Castle Windsor.
  • Project.UI.Web Asp.Net MVC(“演示”)项目 - 它将整个应用程序连接在一起。在Project.Core中实现接口的实现,并将它们(以及来自Project.Data的那些)连接到像Castle Windsor这样的DI框架。

Project.UI.Web follows the following conventions:

Project.UI.Web遵循以下约定:

  • its models are only(!) viewmodels
  • 它的模型只是(!)viewmodels
  • the views consume their own viewmodel (one-view-one-viewmodel)
  • 视图使用自己的视图模型(one-view-one-viewmodel)
  • the controllers just need to validate the input, transforms it into domain objects (as the business logic knows exacly nothing about viewmodels) and delegate the real work (business logic) to the business services.
  • 控制器只需要验证输入,将其转换为域对象(因为业务逻辑对于视图模型一无所知),并将实际工作(业务逻辑)委托给业务服务。

Summary:
If you follow this model it's helpful to focus on Project.Core: that is the real application. It doesn't worry about the real persistence of data nor cares about how does it get presented. It's just about "how-to-do-it". But it's laying out the rules and contracts (interfaces) the other projects must provide implementations for.

简介:如果您遵循此模型,那么关注Project.Core是有帮助的:这是真正的应用程序。它不担心数据的真实持久性,也不关心它是如何呈现的。这只是“怎么做”。但它正在制定其他项目必须提供实施的规则和合同(接口)。

I hope this helps you with how to layout an Asp.Net MVC application!

我希望这可以帮助您如何布局Asp.Net MVC应用程序!

Lg
warappa

Lg warappa

#2


3  

As others have said, it doesn't change much. My apps are typically architected as such:

正如其他人所说,它没有太大变化。我的应用程序通常是这样构建的:

  • Model Layer (Domain and View Models)
  • 模型层(域和视图模型)
  • Repository Layer (data access)
  • 存储库层(数据访问)
  • Service Layer (sometimes implemented as WCF services depending on the app/requirements)
  • 服务层(有时根据应用程序/要求实现为WCF服务)
  • Server side MVC Layer (Asp.net MVC itself)
  • 服务器端MVC层(Asp.net MVC本身)
  • Client side MVVM or MVC (via either Knockout.js, Backbone.js, or Spine.js)
  • 客户端MVVM或MVC(通过Knockout.js,Backbone.js或Spine.js)

In the server side MVC layer, my controller methods are very light. They typically call a method on a service layer object to get some data and pass it along to the client as Json data.

在服务器端MVC层,我的控制器方法非常轻。他们通常在服务层对象上调用一个方法来获取一些数据,并将其作为Json数据传递给客户端。

Because I'm sending Json back, my views are also very light and sparse. Typically just containing script includes and templates which will be rendered with a client side templating library.

因为我要把Json送回去,我的观点也非常轻松和稀疏。通常只包含脚本包含和将使用客户端模板库呈现的模板。

#3


0  

In short: nothing much changes.

简而言之:没有太大的变化。

I'm only familiar with a few presentation patterns: MVP (Model, View, Presenter, common in windows forms/asp.net), MVC (Model, View, Controller) and MVVM (Model, View, View Model, commonly used in WPF/Silverlight).

我只熟悉一些演示模式:MVP(模型,视图,演示者,Windows窗体/ asp.net中常见),MVC(模型,视图,控制器)和MVVM(模型,视图,视图模型,常用于WPF / Silverlight的)。

Link: http://haacked.com/archive/2008/06/16/everything-you-wanted-to-know-about-mvc-and-mvp-but.aspx

链接:http://haacked.com/archive/2008/06/16/everything-you-wanted-to-know-about-mvc-and-mvp-but.aspx

Above link should answer some (if not all) of your questions.

上面的链接应该回答你的一些问题(如果不是全部的话)。

The way I usually write ASP.NET MVC applications is by including at least a Service/Business layer hybrid for CRUD operations (because data access belongs neither on the view model or controller and definitely not in the view!).

我通常编写ASP.NET MVC应用程序的方法是至少包含一个用于CRUD操作的服务/业务层混合(因为数据访问既不属于视图模型,也不属于控制器,绝对不属于视图!)。

#4


-2  

Very basic explanation:

非常基本的解释:

If you create a new MVC application you will automatically get a Controllers, Models and Views folder.

如果您创建新的MVC应用程序,您将自动获得Controllers,Models和Views文件夹。

Your controllers act like your Business Layer
Models are Data Access/Service layer
and Views are the presentation layer.

您的控制器就像您的业务层模型是数据访问/服务层,而视图是表示层。

See http://www.asp.net/mvc for a fully detailed explanation.

有关详细说明,请参见http://www.asp.net/mvc。

#1


36  

I see the Asp.Net MVC part only as the view (or presentation) part of the whole application.

我认为Asp.Net MVC部分仅作为整个应用程序的视图(或演示)部分。

I struggled too with the problem how to structure the app in a proper way.
Following the Onion Architecture I heard about here (and especially the image found here), my solution looks this way:

我也在努力解决如何以适当的方式构建应用程序的问题。按照我在这里听到的洋葱架构(特别是这里找到的图像),我的解决方案看起来像这样:

  • Project.Core
    Business logic/services implementations, entities, interfaces that must be implemented by the other projects (i.e. "IRepository", "IAuthenticationService",...)
  • Project.Core业务逻辑/服务实现,实体,必须由其他项目实现的接口(即“IRepository”,“IAuthenticationService”,......)
  • Project.Data
    DB connection - in my case NHibernate repositories and entity-mappings go here. Implements data-interfaces of Project.Core
  • Project.Data数据库连接 - 在我看来,NHibernate存储库和实体映射都在这里。实现Project.Core的数据接口
  • Project.UI.Web
    The Asp.Net MVC ("presentation") project - it wires the whole app together.
    Has implementations for Interfaces in Project.Core and wires them (and those from Project.Data) up with some DI framework like Castle Windsor.
  • Project.UI.Web Asp.Net MVC(“演示”)项目 - 它将整个应用程序连接在一起。在Project.Core中实现接口的实现,并将它们(以及来自Project.Data的那些)连接到像Castle Windsor这样的DI框架。

Project.UI.Web follows the following conventions:

Project.UI.Web遵循以下约定:

  • its models are only(!) viewmodels
  • 它的模型只是(!)viewmodels
  • the views consume their own viewmodel (one-view-one-viewmodel)
  • 视图使用自己的视图模型(one-view-one-viewmodel)
  • the controllers just need to validate the input, transforms it into domain objects (as the business logic knows exacly nothing about viewmodels) and delegate the real work (business logic) to the business services.
  • 控制器只需要验证输入,将其转换为域对象(因为业务逻辑对于视图模型一无所知),并将实际工作(业务逻辑)委托给业务服务。

Summary:
If you follow this model it's helpful to focus on Project.Core: that is the real application. It doesn't worry about the real persistence of data nor cares about how does it get presented. It's just about "how-to-do-it". But it's laying out the rules and contracts (interfaces) the other projects must provide implementations for.

简介:如果您遵循此模型,那么关注Project.Core是有帮助的:这是真正的应用程序。它不担心数据的真实持久性,也不关心它是如何呈现的。这只是“怎么做”。但它正在制定其他项目必须提供实施的规则和合同(接口)。

I hope this helps you with how to layout an Asp.Net MVC application!

我希望这可以帮助您如何布局Asp.Net MVC应用程序!

Lg
warappa

Lg warappa

#2


3  

As others have said, it doesn't change much. My apps are typically architected as such:

正如其他人所说,它没有太大变化。我的应用程序通常是这样构建的:

  • Model Layer (Domain and View Models)
  • 模型层(域和视图模型)
  • Repository Layer (data access)
  • 存储库层(数据访问)
  • Service Layer (sometimes implemented as WCF services depending on the app/requirements)
  • 服务层(有时根据应用程序/要求实现为WCF服务)
  • Server side MVC Layer (Asp.net MVC itself)
  • 服务器端MVC层(Asp.net MVC本身)
  • Client side MVVM or MVC (via either Knockout.js, Backbone.js, or Spine.js)
  • 客户端MVVM或MVC(通过Knockout.js,Backbone.js或Spine.js)

In the server side MVC layer, my controller methods are very light. They typically call a method on a service layer object to get some data and pass it along to the client as Json data.

在服务器端MVC层,我的控制器方法非常轻。他们通常在服务层对象上调用一个方法来获取一些数据,并将其作为Json数据传递给客户端。

Because I'm sending Json back, my views are also very light and sparse. Typically just containing script includes and templates which will be rendered with a client side templating library.

因为我要把Json送回去,我的观点也非常轻松和稀疏。通常只包含脚本包含和将使用客户端模板库呈现的模板。

#3


0  

In short: nothing much changes.

简而言之:没有太大的变化。

I'm only familiar with a few presentation patterns: MVP (Model, View, Presenter, common in windows forms/asp.net), MVC (Model, View, Controller) and MVVM (Model, View, View Model, commonly used in WPF/Silverlight).

我只熟悉一些演示模式:MVP(模型,视图,演示者,Windows窗体/ asp.net中常见),MVC(模型,视图,控制器)和MVVM(模型,视图,视图模型,常用于WPF / Silverlight的)。

Link: http://haacked.com/archive/2008/06/16/everything-you-wanted-to-know-about-mvc-and-mvp-but.aspx

链接:http://haacked.com/archive/2008/06/16/everything-you-wanted-to-know-about-mvc-and-mvp-but.aspx

Above link should answer some (if not all) of your questions.

上面的链接应该回答你的一些问题(如果不是全部的话)。

The way I usually write ASP.NET MVC applications is by including at least a Service/Business layer hybrid for CRUD operations (because data access belongs neither on the view model or controller and definitely not in the view!).

我通常编写ASP.NET MVC应用程序的方法是至少包含一个用于CRUD操作的服务/业务层混合(因为数据访问既不属于视图模型,也不属于控制器,绝对不属于视图!)。

#4


-2  

Very basic explanation:

非常基本的解释:

If you create a new MVC application you will automatically get a Controllers, Models and Views folder.

如果您创建新的MVC应用程序,您将自动获得Controllers,Models和Views文件夹。

Your controllers act like your Business Layer
Models are Data Access/Service layer
and Views are the presentation layer.

您的控制器就像您的业务层模型是数据访问/服务层,而视图是表示层。

See http://www.asp.net/mvc for a fully detailed explanation.

有关详细说明,请参见http://www.asp.net/mvc。