#StackBounty: #properties #set #accessor Information hiding on Set accessor of property

Bounty: 50

I want to use information hiding with a set accessor of a property. So I want to trigger the set accessor without putting any value in it. In the set acessor is the initialisation of the value, like this :

    /// <summary>
    ///     load short ref controls
    /// </summary>
    /// <param name="reset">(re)set cached list</param>
    public List<ShortRef_Control> ShortRefControls
    {
        // (re)set cached list
        set
        {
            tableDictionary["refcontrols"] = 
              (from src in dataContext.ShortRef_Controls select src).ToList();
        }

        // return cached list
        get
        {
            return tableDictionary.Get<List<ShortRef_Control>>("refcontrols");
        }
    }

Question: Why I want to do this?
Answer: Two reasons: 1. I also want to reset this, I use a cached list of a table with rows from database. When this table is updated, then I want to refresh the cached list. I don’t want to use difficult readable setters for this and so want to hide the setter initialisation. 2. I use key names, typing errors are then possible, keep those names together is a great solution.
What works? Possible not so great solution :
ShortRefControls = null

Is the C# language missing something? Or am I (mis)using properties this way. ShortRefControls = null works but If another collegue of mine is maintaining this code he could get the impression ShortRefControls value is set to null.

The best solution I can come up with is to don’t use properties for this and Use a separate GetShortRefControls() and SetShortRefControls() function.

UPDATE: I (ahum…my collegue 😉 came up with this :

    /// <summary>
    ///     load short ref controls
    /// </summary>
    private void SetShortRefControls() => ShortRefControls = (from src in dataContext.ShortRef_Controls select src).ToList();

This private function is used as the setter value. In some way its behaving like an alias or a wrapper for the setter. Whats wrong with this? Its an extra function that would not really be needed.

C# version x? Is it strange to think that ShortRefControls =< is initializing with no value, is a way to handle this in a future version of C#. The =< characters could be off course any characters, meaning self initialization. To let the setter behave like a setter should, the = says ShortRefControls should get a value, the < says it will be initialised by itself and not passed as value.

UPDATE 2: Well after further thinking I came up with this :

/// <summary>
///     load short ref controls
/// </summary>
/// <param name="set">(re)set content cached list</param>
public List<ShortRef_Control> ShortRefControlList(bool set = false)
{
    // set or reset cache
    if (set)
      tableDictionary["refcontrols"] = 
        (from src in dataContext.ShortRef_Controls select src).ToList();

    // always return content of cache
    return tableDictionary.Get<List<ShortRef_Control>>("refcontrols");
}

So in the end its no property but a single function. I postfixed the methodname with List so we know a list is referenced for clarity.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.