ASP.net MVC 中哪种方法更好?

发布于 2024-12-10 22:35:45 字数 540 浏览 0 评论 0 原文

第一种方法

   /\
  /  \
V/    \ C <=WCF=> {Business Layer (with business logic) <=> ORM <=> Database}
/      \
--------
    M

与第二种方法

   /\
  /  \
V/    \ M <=WCF=> {Business Layer (with business logic) <=> ORM <=> Database}
/      \
--------
    C

主要区别: - 在第一种方法中,业务对象将有 2 个版本。一种在业务层内加载属性,另一种愚蠢版本则很少有视图特定属性由控制器填充。而在第二种方法中,将有一个具有所有属性的类。 - 第一种方法似乎与表示层更加解耦

在这两种方法中,控制器都是起点。 就我从不同人那里收到的观点来看,这两种方法都是有效的方法,并且可以根据开发偏好进行切换。
请分享想法。

First Appraoch

   /\
  /  \
V/    \ C <=WCF=> {Business Layer (with business logic) <=> ORM <=> Database}
/      \
--------
    M

Second Approach

   /\
  /  \
V/    \ M <=WCF=> {Business Layer (with business logic) <=> ORM <=> Database}
/      \
--------
    C

Main Difference:
- In first approach, business object will have 2 versions. One loaded with properties inside business layer and other dumb version with few View specific properties to be populated by Controller. While in second appraoch, there will be one class with all properties.
- First approach seem a bit more decoupled from presentation layer

In both approach, Controller would be starting point.
As far from views I've recevied from different people, both are valid approaches and can be toggled as per development preference.
Please share thoughts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人问我粥可暖 2024-12-17 22:35:45

我们在 ASP.NET MVC 应用程序中使用下一种方法:

  • ViewModels - 聚合视图呈现信息的平面对象
  • 控制器 - 从业务对象(内部有逻辑或由 ORM 生成)构建 ViewModels
  • 模型 - 不存在的业务对象视图中可用

View <- ViewModel <- Controller <-BusinessObject <- Db

您可以从 专业 ASP.NET 设计模式

We are using next approach in asp.net mvc applications:

  • ViewModels - flat objects that aggregate information for view rendering
  • Controllers - build ViewModels from business objects (that have logic inside them or that are generated by ORM)
  • Model - business objects that aren't available in view

View <- ViewModel <- Controller <-BusinessObject <- Db

You can get more details about this approach from Professional ASP.NET Design Patterns

风渺 2024-12-17 22:35:45

第一个。 ViewModel(在三元组中称为“M”)不应该知道视图或业务层中的任何内容。

真正的MVC定义:

  • 模型:业务层、Web服务或其他。
  • 视图:UI
  • 控制器:模型和视图之间的粘合剂。

微软添加了ViewModel以使分离更加清晰。对于初学者来说,视图不应依赖于您的域模型。而且它不应该包含逻辑。

我在这里列出了使用视图模型的主要原因:http://blog.gauffin.org/2011/07/ Three-reasons-to-why-you-should-use-view-models/

The first one. ViewModels (as you refer as "M" in your triad) should not know of anything in the View or in your business layer.

The real MVC definition:

  • Model: Business layer, webservice or whatever.
  • View: UI
  • Controller: Glue between the Model and the View.

Microsoft added the ViewModel to make the separation more clear. For starters, the view shouln't be dependent of your domain models. And it should not contain logic.

I've listed the main reasons to use view models here: http://blog.gauffin.org/2011/07/three-reasons-to-why-you-should-use-view-models/

暮凉 2024-12-17 22:35:45

恕我直言,业务逻辑属于模型。控制器不应该知道也不关心为什么你想要在视图中显示某些内容。

事实上,我认为这是一个最适合这里工作的问题,而不仅仅是绝对的“最佳”。

imho Business logic belongs to Models. Controllers shouldn't know nor care about why you want something displayed in your Views.

In fact, i think it's a matter of what's best for the job here, more than an absolute "best".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文