设置布尔标志的最佳实践

Best practice for setting boolean flags

我正在寻找关于 class 的最佳实践,其中有一个标志决定是否可以对该 class 执行操作。整个 class 将在 WCF REST 服务中返回,并通过 RIA 服务在 Silverlight GUI 中使用,以便为用户设置要启用或禁用的某些操作按钮。我想知道以这种方式设置 class 的最佳做法。例如:

public class SomeCustomerClass
{
    private bool _canResetCustomer;
    public bool CanResetCustomer
    {
        get { return _canResetCustomer; } //TODO: Place GET logic here from DB if it's null
        set { _canResetCustomer = value; }
    }

    if (this._canResetCustomer)
    {
        ResetCustomer();
    }
...

看到我的"TODO"了吗?我需要确定 bool 是否已设置。如果还没有设置,我需要从数据库中的基于数据的规则列表中获取该客户的重置资格。在我看来,有两个选项,我以前都用过:

  1. 定义另一个 bool 来跟踪是否已设置 CanReset,如下所示:

        public bool _canResetSet { get; set; }
    
  2. 将我的 bool 更改为可为 null 的类型。我认为在构造函数中我必须用 _canResetCustomer = null 实例化对象。也许吧?

不确定我为什么要问这个问题,除了我可能只是害羞的可空类型。这整个困境是否说明了我设计事物的方式的其他问题?我也经常在 webforms 应用程序中使用这种标记方法。

选择可为空。如果它的 HasValue 等于 false,去检查数据库。没有必要在构造函数中将其设置为 null,因为它是默认值。