- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我一直在阅读一些关于异常及其使用的问题和答案。似乎强烈认为应该只针对异常、未处理的情况提出异常。所以这让我想知道验证是如何与业务对象一起工作的。
假设我有一个业务对象,其中包含对象属性的 getter/setter。假设我需要验证该值在 10 到 20 之间。这是一个业务规则,所以它属于我的业务对象。所以这对我来说似乎意味着验证代码在我的 setter 中。现在我将我的 UI 数据绑定(bind)到数据对象的属性。用户输入 5,所以规则需要失败,不允许用户移出文本框。 . UI 数据绑定(bind)到属性,因此将调用 setter 、检查规则并失败。如果我从我的业务对象中提出一个异常来说明规则失败,UI 会选择它。但这似乎与异常的首选用法背道而驰。鉴于它是二传手,您不会真正为二传手获得“结果”。如果我在对象上设置另一个标志,则意味着 UI 必须在每次 UI 交互后检查该标志。
那么验证应该如何进行?
编辑:我可能在这里使用了一个过度简化的例子。 UI 可以轻松处理上面的范围检查之类的事情,但是如果验证更复杂怎么办,例如业务对象根据输入计算一个数字,如果计算出的数字超出范围,则应拒绝该数字。这是不应该出现在 UI 中的更复杂的逻辑。
还考虑根据已输入的字段输入更多数据。例如,我必须在订单上输入一个项目以获取某些信息,如手头库存、当前成本等。用户可能需要此信息来决定进一步输入(例如要订购多少单位),或者可能需要它来订购待进一步验证。如果项目无效,用户是否应该能够输入其他字段?有什么意义呢?
最佳答案
您想深入研究 Paul Stovell 的杰出工作关于数据验证。他在this article中一次性总结了自己的想法.我恰好分享了他对此事的观点,我在my own libraries中实现了这一点.
用 Paul 的话说,这是在 setter 中抛出异常的缺点(基于 Name
属性不应为空的示例):
- There may be times where you actually need to have an empty name. For example, as the default value for a "Create an account" form.
- If you're relying on this to validate any data before saving, you'll miss the cases where the data is already invalid. By that, I mean, if you load an account from the database with an empty name and don't change it, you might not ever know it was invalid.
- If you aren't using data binding, you have to write a lot of code with
try/catch
blocks to show these errors to the user. Trying to show errors on the form as the user is filling it out becomes very difficult.- I don't like throwing exceptions for non-exceptional things. A user setting the name of an account to "Supercalafragilisticexpialadocious" isn't an exception, it's an error. This is, of course, a personal thing.
- It makes it very difficult to get a list of all the rules that have been broken. For example, on some websites, you'll see validation messages such as "Name must be entered. Address must be entered. Email must be entered". To display that, you're going to need a lot of
try/catch
blocks.
这里是替代解决方案的基本规则:
- There is nothing wrong with having an invalid business object, so long as you don't try to persist it.
- Any and all broken rules should be retrievable from the business object, so that data binding, as well as your own code, can see if there are errors and handle them appropriately.
关于c# - 业务对象、验证和异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/88541/
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我在这里感觉有些不对劲,所以我希望社区提供意见 - 也许我以错误的方式处理这个问题...... 问:是否适合使用传统的基础架构日志框架(如 log4net)来记录业务事件? 当我说业务事件时,我的意思
技术也好,业务也罢; 01 【什么是业务?】 对于那些久经职场的人,也很难一句话说明白; 业务,作为工作中绝对的核心点,即便在一个公司待的足够久,
三天研发,两天设计; 01 【优先做设计方案】 职场中的那些魔幻操作,研发最烦的是哪个? 作为一个数年且资深的互联网普通开发,可以来说明一下为什么
业务、系统、接口(interface)、持久化类是什么意思?用一些例子解释一下? 最佳答案 业务可能是应用程序中所有功能部分发生的部分(即计算或规则) 系统是您的操作系统 接口(interface),
我无法创建带有指向移动应用的外部链接的简单广告。我已正确设置访问权限,可以创建广告系列、广告集、加载图像,但在创建广告期间出现错误: Ads and ad creatives must be asso
我是软件工程专业的学生,现在我正在为我的期末项目工作,安排在交易日进行商务配对。 这个想法是将卖家(开发人员)和买家(有经济能力的人)在一起。 算法应该像“快速约会”。 假设我有 15 张 table
我们只需按照以下说明在 AWS Cloudformation 上部署企业 WhatsApp API: AWS WhatsApp API 所以一切正常,部署正确完成,问题出在“SSL 配置”选项上,我们
我的应用因为以下原因被拒绝了 Guideline 3.2 - Business We found that your app is not appropriate for the App Store
您好,我想在我的网络应用程序中使用 WhatsApp 业务 API。我已经在 postman 中测试过了。 每当在 WhatsApp 选项卡下的 Facebook 业务页面中运行示例 curl 代码时
我是 Skype for Business 技术的新手,我正在尝试部署一个我愿意与 Skype WebSDK 和 AppSDK 一起使用的服务器。 起初我尝试使用Skype进行在线商务,但websdk
Apple 开发人员以此为由拒绝了我的应用。 “业务 - 3.1.1您的应用程序包含一个帐户注册功能,该功能被视为对外部机制的访问,以便在应用程序中使用购买或订阅。此功能不符合 App Store 审
我正在玩 Realm for Android。 我喜欢自动更新对象的想法,但我对它的软件架构有顾虑。 我已经看到许多提议的架构都指定了一个层来处理数据/数据库访问,理想情况下,更高层不会知道有关数据库
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 10 个月前。 Impro
我正在尝试设置 Whatsapp 业务 API。 引用Link用于设置。 我正在使用 Windows 10 操作系统。 -- 仅供引用。 在初始设置部分,使用命令 docker-compose up
我正在尝试使用 Facebook Business SDK 创建一个 facebook Adaccount .但是当我使用方法 createAdAccount 时,它会给我一个错误。请参阅下面的图片。
我想写一个概念证明 MonoMac使用 c# 和 Razor 呈现 html“ View ”的应用程序。 这可能吗? 如果没有,是否有人推荐与 Razor 的简单性相比的任何其他模板引擎。如果必须的话
无法解析 ':business:diary@debug/compileClasspath': Could not resolve project :fun:push. 的依赖关系 无法解析项目:fun
当我尝试在 Visual Studio Professional 2015 14.0.23107.0 中打开某些 XML 文件时,XML 编辑器出现白屏并显示以下文本: 为什么我不能编辑这个文件?此项
我是一名优秀的程序员,十分优秀!