Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monodroid: Where should I put configuration settings?

From Miguel de Icaza:

We use a library profile that is better suited for mobile devices, so we removed features that are not necessary (like the entire System.Configuration stack, just like Silverlight does).

After years of .NET development, I'm accustomed to storing configuration settings in web.config and app.config files.

  • When using Mono for Android, where should I put my configuration settings?
    • If it matters, I'd like to store different configuration settings for different build configurations as well.
like image 954
Jim G. Avatar asked Jan 18 '13 14:01

Jim G.


3 Answers

there's a Xamarin centric AppSetting reader: https://www.nuget.org/packages/PCLAppConfig pretty useful for continuous delivery (so a deployment server such as octopus allows to alter your config file for each environment with values stored on the cd server)

there's a Xamarin centric AppSetting reader available at https://www.nuget.org/packages/PCLAppConfig it is pretty useful for continuous delivery;

use as per below:

1) Add the nuget package reference to your pcl and platforms projects.

2) Add a app.config file on your PCL project, then as a linked file on all your platform projects. For android, make sure to set the build action to 'AndroidAsset', for UWP set the build action to 'Content'. Add you settings keys/values: <add key="config.text" value="hello from app.settings!" />

3) Initialize the ConfigurationManager.AppSettings on each of your platform project, just after the 'Xamarin.Forms.Forms.Init' statement, that's on AppDelegate in iOS, MainActivity.cs in Android, App in UWP/Windows 8.1/WP 8.1:

ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);

3) Read your settings : ConfigurationManager.AppSettings["config.text"];

like image 194
Ben Ishiyama-Levy Avatar answered Oct 11 '22 13:10

Ben Ishiyama-Levy


I would probably recommend using shared preferences and compilation symbols to manage different configurations. Below is an example of how you can use a preferences file to add or change keys based on the compilation symbols. Additionally, you could create a separate preferences file that is only available for a particular configuration. Because these keys are not available on all configurations, make sure to always perform checks for them before using.

var prefs = this.GetSharedPreferences("Config File Name", FileCreationMode.Private);
var editor = prefs.Edit();

#if MonoRelease
editor.PutString("MyKey", "My Release Value");
editor.PutString("ReleaseKey", "My Release Value");
#else
editor.PutString("MyKey", "My Debug Value");
editor.PutString("DebugKey", "My Debug Value");
#endif

editor.PutString("CommonKey", "Common Value");
editor.Commit();
like image 39
Mike Stonis Avatar answered Oct 11 '22 12:10

Mike Stonis


We have had exactly the same problem in our current project. My first impulse was to put the configuration in a sqlite key-value table but then my internal customer reminded me the main reason for a configuration file - it should support simple editing.
So instead we created an XML file and put it there:

string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

And access it using these properties:

public string this[string key]
    {
        get
        {
            var document = XDocument.Load(ConfigurationFilePath);
            var values = from n in document.Root.Elements()
                       where n.Name == key
                       select n.Value;
            if(values.Any())
            {
                return values.First();
            }
            return null;
        }
        set
        {
            var document = XDocument.Load(ConfigurationFilePath);
            var values = from n in document.Root.Elements()
                       where n.Name == key
                       select n;
            if(values.Any())
            {
                values.First().Value = value;
            }
            else
            {
                document.Root.Add(new XElement(key, value));
            }
            document.Save(ConfigurationFilePath);
        }
    }
}

via a singleton class we call Configuration so for .NET developers it is very similar to using the app.config files. Might not be the most efficient solution but it gets the job done.

like image 3
Shy Avatar answered Oct 11 '22 11:10

Shy