Search code examples
c#asp.netwcfsilverlightria

Best practice for setting boolean flags


I'm looking for best practices in regards to having a class which has a flag which decides whether an operation can be performed on that class or not. This entire class will be returned in WCF REST services as well used in a Silverlight GUI via RIA Services in order to set certain action buttons to be enabled or disabled for the user. I want to know best practices for setting up a class in this way. For example:

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();
    }
...

See my "TODO"? I need to determine if the bool has been set. If it hasn't been set, I need to get the eligibility of this customer for reset from a list of data-based rules from the database. The way I see it, there are two options, both of which I've used before:

  1. Define another bool which tracks whether CanReset has been set or not, like so:

        public bool _canResetSet { get; set; }
    
  2. Change my bool to a nullable type. I think in the constructor I'd have to instantiate the object with _canResetCustomer = null. Maybe?

Not sure why I'm asking this question except that maybe I'm just shy of nullable types. Does this entire dilemma speak to other issues with the way I design things? I commonly use this method of flagging in webforms applications as well.


Solution

  • Go with nullable. If its HasValueequals false, go check the db. Setting it to null in the constructor isn't necessary, as it is the default.