本文介绍了ASP .NET MVC 架构如何融入传统的多层架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

似乎 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 视图和控制器以及视图模型 -are- 表示层

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

推荐答案

我将 Asp.Net MVC 部分 视为视图(或表示)部分整个应用程序.

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

我也为如何以适当的方式构建应用程序的问题而苦苦挣扎.
遵循洋葱架构,我在这里(尤其是在此处找到的图片),我的解决方案如下所示:

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
    其他项目必须实现的业务逻辑/服务实现、实体、接口(即IRepository"、IAuthenticationService"等)
  • 项目.数据
    数据库连接 - 就我而言,NHibernate 存储库和实体映射放在此处.实现 Project.Core 的数据接口
  • Project.UI.Web
    Asp.Net MVC(演示")项目 - 它将整个应用程序连接在一起.
    在 Project.Core 中有接口的实现,并将它们(以及来自 Project.Data 的接口)与一些 DI 框架(如 Castle Windsor)连接起来.

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

Project.UI.Web follows the following conventions:

  • 它的模型只是(!)视图模型
  • 视图使用它们的自己的视图模型(one-view-one-viewmodel)
  • 控制器只需要验证输入,转换域对象(作为业务逻辑对视图模型一无所知)并将实际工作(业务逻辑)委托给业务服务.
  • its models are only(!) viewmodels
  • the views consume their own 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.

总结:
如果您遵循此模型,专注于 Project.Core 会很有帮助:真正的应用程序.它不关心数据的真正持久性,也不关心它是如何呈现的.这只是关于如何做".但它列出了其他项目必须为其提供实现的规则和契约(接口).

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.

我希望这对您如何布局 Asp.Net MVC 应用程序有所帮​​助!

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

LG
瓦拉帕

这篇关于ASP .NET MVC 架构如何融入传统的多层架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:21