1

I would like to get some advise about getting and setting multiple values in one property. I have a lot of the same properties that get and set an int value from app settings, based on a selected profile (so the value will be retrieved from the app settings in the getter and in the setter the new value will be saved to the app settings).

Currently, all properties look like the following example code:

public int NumberValue
{
    get 
    { 
        if (_profile == MainProfile.ProfileOne)
        {
            return AppSettings.NumberValue1; 
        }
        else if(_profile == MainProfile.ProfileTwo)
        {
            return AppSettings.NumberValue2; 
        }
        else
        {
            return AppSettings.NumberValue3;  
        }
    }
    set
    {
        if (_profile == MainProfile.ProfileOne)
        {
            AppSettings.NumberValue1 = value; 
        }
        else if(_profile == MainProfile.ProfileTwo)
        {
            AppSettings.NumberValue2 = value;
        }
        else
        {
            AppSettings.NumberValue3 = value; 
        }

        SaveAppSettings();
        NotifyPropertyChanged();
    }
}

I would like to know if there is an efficient way to rewrite these properties in a more efficient way.

9
  • 2
    Have you thought about doing different classes? FooProfileOne, FooProfileTwo... with a base class of Foo or interface of IFoo ? Commented Oct 31, 2017 at 9:08
  • 2
    Why don't you consider using your own ConfigurationSection implementation? It would be much cleaner. You can also have a base class containing these properties and can have an extended class for each profile. Commented Oct 31, 2017 at 9:09
  • What exactly is MainProfile ? Is it an enum or a class ? Commented Oct 31, 2017 at 9:15
  • @fabjan it's a enum. Commented Oct 31, 2017 at 9:15
  • 1
    Btw, you know you could have your profiles in different config files and just load the relevent with ConfigurationManager.OpenExeConfiguration("xxxx.config"); ? Commented Oct 31, 2017 at 9:17

1 Answer 1

2

One approach could be to see your configuration more abstract. Implement a layer between your settings and business logic which will consume the values. This layer should handle the profiles and returns what you need, instead of a lot if-else-switches in a property getter and setter, put them into an object.

The new layer could just be a Configuration object that needs a MainProfile on initialization and implements a IConfiguration interface that provides GetNumberValue and SaveNumberValue methods. The implementation of your IConfiguration will contain your logic you have in actual getter and setter methods. Now this Configuration object could be used in your getter and setter instead of if-else-switches.

public interface IConfiguration
{
    int GetNumberValue();

    void SaveNumberValue(int number);
}

Based on the possible solution above, you could add a method SetProfile to the interface and decide in your logic for which profile the value should be stored or read.

It is also possible to create separate classes that will represent different ConfigurationSections in your App.config file or you create for each profile an own App.config file. Then the Configuration object could handle to load the necessary file.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Chw, Indeed your solution was already mentioned in comments. I will mark this as answer anway because it will resolve my problems. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.