我应该从UI层返回到业务层什么? [英] What should I return from the UI layer to the business layer?

查看:83
本文介绍了我应该从UI层返回到业务层什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个ASP.NET应用程序,它具有一个UI层,业务逻辑层和数据访问层.从我的数据层,我将业务对象返回到业务逻辑层,并将其传递给UI层.但是,我不确定要对UI层中的数据执行保存/插入操作.

I am writing an ASP.NET application which I have a UI layer, business logic layer, and data access layer. From my data layer I return business objects to the business logic layer and pass these on to the UI layer. However, I'm not sure what to do when I want to perform a save/insert with data from the UI layer.

我应该在UI层上创建业务对象并传递到业务层,还是应该在业务层中创建它?

Should I create the business object at the UI layer and pass on to the business layer or should I be creating it in the business layer?

非常感谢

推荐答案

我同意crunchdog的要求-除了最琐碎的Web应用程序之外,所有其他应用程序都应具有扁平化的业务对象形式,专门用于UI/视图层.有时,这被称为视图模型"类,通常只包含UI层可以从中获取并直接放入而无需进行验证的几个字符串属性. (请参见 asp.net mvc )

I agree with crunchdog-- for all but the most trivial web applications you should have a flattened form of your business objects specifically for your UI/view layer. Sometimes this is referred to as a View Model class and generally contains nothing more than several string properties that the UI layer can get from and put to directly without worry of validation. (see asp.net mvc)

一方面,这使UI层更整洁,更简单,而使它致力于显示数据,而不是遍历对象结构,检查和解释null等.

For one, this keeps the UI layer cleaner and simpler, leaving it to put it's efforts toward displaying the data rather than traversing object structures, checking for and interpreting nulls, etc.

这也为业务层提供了对这些字符串值进行验证的机会,如果它们无效,则返回输入的值.例如,当您的服务器必须在日期字段中处理无效的日期时,这可以节省编码的麻烦.识别无效值的业务层可以将它们正确返回,并附带正确的错误消息.如果您处理的只是业务/域对象,那么输入的某些值可能并不总是适合用于保存它们的对象.

This also gives the business layer the opportunity to do validation upon those string values, returning the values as entered if they are invalid. This can save coding frustration when, for example, your server has to handle an invalid date in a date field. The business layer, recognizing the invalid values, can return them exactly as received along with the proper error messages. If all you dealt with were business/domain objects, then some values entered may not always fit the objects intended to hold them.

指定一个用于在业务/域对象和UI对象/视图模型之间来回映射值的类也可以帮助您.这有助于使业务层关注点清晰地分开.

It can also help to have a class designated for mapping values back and forth between the business/domain objects and the UI object/view model. This helps keep the business layer concerns cleanly separated.

这篇关于我应该从UI层返回到业务层什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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