业务验证逻辑code气味 [英] Business Validation Logic Code Smell

查看:154
本文介绍了业务验证逻辑code气味的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code:

partial class OurBusinessObject {
    partial void OnOurPropertyChanged() {
        if(ValidateOurProperty(this.OurProperty) == false) {
            this.OurProperty = OurBusinessObject.Default.OurProperty;
        }
    }
}

也就是说,当 OurProperty 的值 OurBusinessObject 被改变,如果该值无效,设置它是默认值。这种模式令我code的气味,但其他人在这里(我的雇主)不同意。你有什么想法?

That is, when the value of OurProperty in OurBusinessObject is changed, if the value is not valid, set it to be the default value. This pattern strikes me as code smell but others here (at my employer) do not agree. What are your thoughts?

编辑补充:我一直在问,增加了对为什么这被认为是好的解释。当时的想法是,与其具有业务对象的生产验证数据,业务对象可以验证自己的属性,并在情况设置清洁的默认值时,验证失败。此外,有人认为,如果验证规则的改变,业务对象生产者不会有改变自己的逻辑,业务对象将采取验证和清理数据的照顾。

Edited to add: I've been asked to add an explanation for why this is thought to be okay. The idea was that rather than having the producers of the business object validate the data, the business object could validate its own properties, and set clean default values in cases when the validation failed. Further, it was thought, if the validation rules change, the business object producers won't have to change their logic as the business object will take care of validating and cleaning the data.

推荐答案

这绝对可怕。祝你好运试图调试问题的生产。它可导致唯一的事情是掩盖错误,这将只是弹出其他地方,在那里将不会很明显,在所有的,他们都来自哪里。

It absolutely horrible. Good luck trying to debug issues in Production. The only thing it can lead to is to cover bugs, which will just pop up somewhere else, where it will be not obvious at all where they are coming from.

这篇关于业务验证逻辑code气味的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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