在 C# 中使用属性或方法公开业务规则?

发布于 2024-08-24 09:01:17 字数 1194 浏览 4 评论 0原文

我正在编写一个类来封装一些业务规则,每个规则都由一个布尔值表示。该类将用于处理 InfoPath 表单,因此规则通过使用 XPath 操作在全局 XML 数据结构中查找值来获取当前程序状态。将这些规则公开给调用者的最佳(最惯用)方式是什么——属性还是公共方法?

使用属性调用

Rules rules = new Rules();
if ( rules.ProjectRequiresApproval ) {
    // get approval
} else {
    // skip approval
}

使用方法调用

Rules rules = new Rules();
if ( rules.ProjectRequiresApproval() ) {
    // get approval
} else {
    // skip approval
}

规则类将规则公开为属性

public class Rules() {
    private int _amount;
    private int threshold = 100;

    public Rules()  {
        _amount = someExpensiveXpathOperation;
    }

    // rule property
    public bool ProjectRequiresApproval {
        get { return _amount > threshold }
    }
}

规则类将规则公开为方法

public class Rules() {
    private int _amount;
    private int threshold = 100;

    public Rules()  {
        _amount = someExpensiveXpathOperation;
    }

    // rule method
    public bool ProjectRequiresApproval() {
        return _amount > threshold;
    }
}

优点是什么以及其中一种相对于另一种的缺点?

I'm writing a class to encapsulate some business rules, each of which is represented by a boolean value. The class will be used in processing an InfoPath form, so the rules get the current program state by looking up values in a global XML data structure using XPath operations. What's the best (most idiomatic) way to expose these rules to callers -- properties or public methods?

Call using properties

Rules rules = new Rules();
if ( rules.ProjectRequiresApproval ) {
    // get approval
} else {
    // skip approval
}

Call using methods

Rules rules = new Rules();
if ( rules.ProjectRequiresApproval() ) {
    // get approval
} else {
    // skip approval
}

Rules class exposing rules as properties

public class Rules() {
    private int _amount;
    private int threshold = 100;

    public Rules()  {
        _amount = someExpensiveXpathOperation;
    }

    // rule property
    public bool ProjectRequiresApproval {
        get { return _amount > threshold }
    }
}

Rules class exposing rules as methods

public class Rules() {
    private int _amount;
    private int threshold = 100;

    public Rules()  {
        _amount = someExpensiveXpathOperation;
    }

    // rule method
    public bool ProjectRequiresApproval() {
        return _amount > threshold;
    }
}

What are the pros and cons of one over the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洋洋洒洒 2024-08-31 09:01:17

这一切都归结为向使用该业务逻辑的人传达语义。

属性是特定对象的内在价值。您的示例中的 Rules 是一个业务逻辑引擎,因此它公开的任何属性都应应用于该引擎的状态和行为,而不是应用于它处理的数据。因此,ProjectRequiresApproval() 是引擎应用于外部实体的操作。

另一方面,如果有 Rules.GetProcess() 方法,它将返回一个 Process 对象,RequiresAproval 可以是该对象的属性。

It all boils down to conveying semantic to whoever consumes that business logic.

A property is an intrinsic value of a particular object. Rules in your example is a business logic engine, so any properties it exposes should apply to the state and behavior of that engine, not to the data that was processed by it. As such, ProjectRequiresApproval() is an action that the engine applies to an external entity.

If on the other hand, there was Rules.GetProcess() method, that would return a Process object, RequiresAproval can be property on that object.

榆西 2024-08-31 09:01:17

首先,您需要将 get{} 包装在属性逻辑周围,并且经验法则(无论如何对我来说)应该是方法可以更改类的内容,或执行某种业务逻辑,而属性公开有关班级的信息。

对于您的使用,我建议使用一个属性,因为值已经存在,并且您正在创建所谓的派生属性。

另外,您的代码可以重构为

return _amount < threshold;

Firstly, you would need to wrap get{} around the property logic, and the rule of thumb (for me, anyway) should be that a method can change the content of a class, or performs some kind of business logic, whereas a property exposes information about the class.

For your usage, I would suggest a property as the values already exist and you are creating what is knowns as a derived property.

Also, your code can be refactored to

return _amount < threshold;
桃酥萝莉 2024-08-31 09:01:17

我同意安东尼·科赫的观点。当我需要执行更改类状态的复杂计算时,我会使用方法。如果需要对类的当前状态进行简单的计算或数据检索,并且数据已存在于该状态并且不需要更改,则属性似乎更合适。

I agree with Antony Koch. I use methods when I need to perform a complex calculation that changes the state of the class. If a simple calculation or data retrieval is needed for the current state of the class where the data already exists in that state and does not need to be changed, a property seems more suitable.

偏爱你一生 2024-08-31 09:01:17

根据属性与方法 MSDN 上的指南(诚然适用于旧版本的 Visual Studio),听起来方法可能比您的情况更适合您的情况(假设规则比阈值更复杂)。

对这个问题的更多研究揭示了以下内容:

  • Ken Browning 关于属性与属性的优秀答案一般方法
  • 博客文章 Bill Wagner(《Effective C#》的作者)介绍了使用属性时应满足哪些条件
  • 几个开源规则引擎的链接 (NxBREDrools.NET),这样你就可以看到实施业务规则的替代方案。

According to the Properties vs. Methods guidelines on MSDN (admittedly for an older version of Visual Studio), it sounds like methods might be more appropriate than properties in your case (assuming the rules get more complex than thresholds).

A bit more poking around on this question revealed the following:

  • An excellent answer from Ken Browning on properties vs. methods in general
  • A blog post from Bill Wagner (author of Effective C#) on what conditions should be true in order to use a property
  • Links to a couple of open-source rules engines (NxBRE and Drools.NET) so you can see alternatives for implementing business rules.
孤檠 2024-08-31 09:01:17

这是一个真实的例子吗?我问这个问题是因为我觉得规则对象可能有金额和阈值很奇怪。我提到这一点是因为我认为这是答案的一部分。我认为您的规则是一个命名空间,项目是一个对象,在我看来,是否需要批准将是项目类的属性。

Is that a real example? I ask because I find it bizarre that a Rules object might have an amount and a threshold. I'm mentioning this because I think it's part of the answer. I would imagine your Rules to be a namespace and a Project to be an object, and whether or not it requires approval would be a property of the Project class in my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文