【发布时间】:2014-01-02 12:39:55
【问题描述】:
我有以下类可以改变某个查询的结果:
public class UniversalPrincipalQueryOptions
{
public Boolean IncludeProperties { get; set; }
public Boolean IncludeOrganizationUnits { get; set; }
public Boolean IncludeManagers { get; set; }
public static UniversalPrincipalQueryOptions All
{
get
{
return new UniversalPrincipalQueryOptions { IncludeOrganizationUnits = true, IncludeProperties = true, IncludeManagers = true };
}
}
public static UniversalPrincipalQueryOptions None
{
get
{
return new UniversalPrincipalQueryOptions { IncludeProperties = false };
}
}
public UniversalPrincipalQueryOptions()
{
this.IncludeProperties = true;
}
}
问题是,当IncludeProperties 为false 时将IncludeOrganizationUnits 设置为true 在我的情况下没有意义,IncludeManagers 也是如此,这也取决于将IncludeOrganizationUnits 设置为true .
我认为这不是最好的方法。所以我的问题是,我可以使用什么模式来改进这个类并让其他人更容易使用我的类?
更新:
好的,所以我最终切换到使用 Flags 属性装饰的 enum:
[Flags]
public enum UniversalPrincipalQueryOptions
{
None = 0,
IncludeProperties = 1,
IncludeOrganizationUnits = 2 | IncludeProperties,
IncludeManagers = 4 | IncludeOrganizationUnits
}
【问题讨论】:
-
"true-ish" 在 javascript 中是有意义的(通常被称为truthy),但 true-ish 对 c# 布尔值意味着什么??
-
你的问题是什么?
-
不是答案:但这种跨规则强制执行是 CodeContract 库示例使用的常用示例。
-
@ken2k:我已经更新了帖子,并在最后包含了我的问题。
-
@Kassem 您的方案是否需要用户可以设置布尔值之一的值?
标签: c# .net class properties