在 C# 中使用属性或方法公开业务规则?
我正在编写一个类来封装一些业务规则,每个规则都由一个布尔值表示。该类将用于处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这一切都归结为向使用该业务逻辑的人传达语义。
属性是特定对象的内在价值。您的示例中的
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 aProcess
object,RequiresAproval
can be property on that object.首先,您需要将 get{} 包装在属性逻辑周围,并且经验法则(无论如何对我来说)应该是方法可以更改类的内容,或执行某种业务逻辑,而属性公开有关班级的信息。
对于您的使用,我建议使用一个属性,因为值已经存在,并且您正在创建所谓的派生属性。
另外,您的代码可以重构为
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
我同意安东尼·科赫的观点。当我需要执行更改类状态的复杂计算时,我会使用方法。如果需要对类的当前状态进行简单的计算或数据检索,并且数据已存在于该状态并且不需要更改,则属性似乎更合适。
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.
根据属性与方法 MSDN 上的指南(诚然适用于旧版本的 Visual Studio),听起来方法可能比您的情况更适合您的情况(假设规则比阈值更复杂)。
对这个问题的更多研究揭示了以下内容:
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:
这是一个真实的例子吗?我问这个问题是因为我觉得规则对象可能有金额和阈值很奇怪。我提到这一点是因为我认为这是答案的一部分。我认为您的规则是一个命名空间,项目是一个对象,在我看来,是否需要批准将是项目类的属性。
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.