在ASP.NET MVC Web应用程序视图逻辑层和业务逻辑之间的混淆 [英] Confusion between view logic and domain logic in a ASP.NET MVC Web Application

查看:137
本文介绍了在ASP.NET MVC Web应用程序视图逻辑层和业务逻辑之间的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我越来越糊涂域/应用逻辑和用户界面逻辑之间。为了说明什么,我想明确,我会在下面描述一个假想的程序说明目的:

I am getting confused between domain/application logic and User Interface logic. To illustrate what I am trying to nail down, I will describe an imaginary program below for illustration purposes:

(1)
想象一下,一组3个级联下拉菜单的小应用程序。当你选择一个下拉菜单就会触发一个jQuery的Ajax找哪家最终创下了MVC的控制器,提供了$ P $的设定值pviously选择下拉。控制器返回下一个下拉的允许选择。该javacript(视图)安排这些结果为下拉。等等。所以,每次选择一个下拉的时候,下一个被填充。

(1) Imagine a small application with a set of 3 cascading drop-downs. As you select one dropdown it triggers a jQuery Ajax GET which ends up hitting a MVC controller, supplying the selected value of the previously selected drop-down. The controller returns the allowable choices for the next drop-down. The javacript (in the view) arranges these results into a drop-down. and so on. So each time you select a drop-down, the next one gets populated.

(2)
现在,在扳手投掷。有一些例外。比方说,如果用户选择富,或在第一个下拉列表BAR,那么behavor变化,使第二个下拉被禁止,而THRID下拉菜单会显示一个texbox来代替。

(2) Now throwing in a wrench.. There are some exceptions. Lets say if the user selects "FOO" or "BAR" in the first dropdown, then the behavor changes, so that the second dropdown is disabled, and the thrid dropdown will show a texbox instead.

我的问题是,在MVC的背景下,究竟是什么决定的逻辑适当的地方吗?比如code就像我在(2)解释说,负责做出这些决定。我一直把这个最方便的地方是正确的观点的JavaScript。我只是写JavaScript来测试,如果第一个框是富或BAR,那么,禁用第二个dropwdown,并换出第三个下拉菜单的文本框。但是,这并不觉得我挺合适的。因为现在看来似乎应该是业务逻辑因此code应领域层属于​​某个地方。但是,这并不感到很正确两种。

My questions is, in the context of MVC, what is the appropriate place for this "decision" logic? Such as the code that is responsible for making these decisions like I explained in (2). The most convenient place that I have been putting this was right in the javascript of the view. I simply wrote javascript to test if the first box is "FOO" or "BAR" then, disable the second dropwdown, and swap out the third dropdown for a textbox. But this does not feel quite right to me. Because It seems like it should be business logic therefore the code should belong in a domain layer some place. But that does not feel quite right either.

所以,我觉得我在圈子里我要去。可有一个人一些启发这个小设计呢?

And so I feel like I am going in circles. Can some one shed some light on this little design?

推荐答案

如果没有分裂太多毛或变得过于狂热上的必须做些什么来保持图案纯色 ...

Without splitting too many hairs or getting too fanatical on WHAT MUST BE DONE TO KEEP THE PATTERN PURE...

显然,控制器知道这种变化必须发生,因为它会被同时处理结果的情况下(下拉选择或文本输入)。所以把与此在控制器逻辑的不是一种罪过。

Obviously the Controller knows that this change must occur, as it will be handling both resulting cases (drop down selections or text entry). So putting logic relating to this in the Controller is not a sin.

也同样为明显的是,查看必须改变它取决于第一下拉的内容如何显示。虽然行为的这种混合不正是最好的UI体验,如果要求,必须我能想象,那么这个逻辑必须存在于用户界面在一定程度上。但是,天啊伙计们,这是一个的网站的,我们在这里讨论的。你真的想从JavaScript删除所有逻辑和其移动到一个控制器方法?视图是决定如何显示数据,这是它的工作,等等的不能是一种罪过

It is also equally as obvious that the View must change how it displays depending on the contents of the first dropdown. While this mixing of behaviors isn't exactly the best UI experience I could imagine, if requirements must, then the logic for this must exist to some extent in the UI. But, jeez guys, this is a website we're talking about here. Do you really want to remove all logic from javascript and move it into a controller method? The View is deciding on how to display data, which is its job, and so cannot be a sin.

要避免在股权被烧伤的真正办法是重新设计,以避免在首位的争论。或者,只是code,并母狗你糟糕的设计要求在一个啤酒。

The real way to avoid getting burnt at the stake is to redesign to avoid the controversy in the first place. Or, just code it and bitch about your lousy design requirements over a beer.

这篇关于在ASP.NET MVC Web应用程序视图逻辑层和业务逻辑之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆