Good practise of storing and using AppConfig class among WPF MVVM application

146 views Asked by At
  • Assume that I have application that needs to use AppConfig values.
  • There could be OptionsView that could allow to change some values during runtime and save them.
  • moreover others ViewModels use AppConfig values during runtime. E.g there is LogViewModel that can enlist logs and according to AppConfig value add additional infos e.g. timestamp of every log.

I would like to know what is the good practise of using AppConfig.

Should I consider Preparing one AppConfig Class which could implement IAppConfig and Inject it into every ViewModel - in such situation I should inject it via constructor and use like with wrapped interface - provider:

private readonly IAppConfigProvider _appConfigProvider;

    private IAppConfigProvider AppConfigProvider
    {
        get
        {
            return _appConfigProvider.ProvideAppConfig();
        }
    }


public interface IAppConfigProvider 
{
    IAppConfig ProvideAppConfig();
    void SetAppConfig(IAppConfig mainConfig);
}

Thanks to above construction I ensure that every ViewModel or Service that has Injected AppConfig has same instance of AppConfig

However, is that solution is not an overkill, overengineering?

Maybe I should prepare static AppConfig class. I know that in second case I cannot use mocking via interface.

public class AppConfig : IAppConfig
{

    public AppConfig()
    {
        var exePath = Assembly.GetExecutingAssembly().Location;
        _config = ConfigurationManager.OpenExeConfiguration(exePath);
    }

    public bool SampleProperty
    {
        get { return GetSettings(AppConfigKey.SampleProperty.GetName()) == "1"; }
        set
        {
            SetSettings(AppConfigKey.SampleProperty.GetName(), Convert.ToInt32(value).ToString(CultureInfo.InvariantCulture));
        }
    }
}

Which solution would be better?

0

There are 0 answers