业务验证逻辑code气味气味、逻辑、业务、code

2023-09-03 04:48:38 作者:该走的走该留的留

考虑以下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.