Menu

Strongly Typed Settings for Xamarin Android

28 March 2017

Recently I've been dabbling a bit in the mobile development space. Ignorantly I tried to add an App.config to my Xamarin Android project, but there was no option available in the Add New Item dialog for it... Hmmm...?

Where on earth do I store configuration settings for your mobile application?

I was a little bit stumped, so I did what any self-respecting developer will do, and started googling. Here is some of the good suggestions that people had:

Use the #if preprocessor directive to build up your configuration settings.

Use a library called PCLAppConfig , that allows you to add App.config as an AndroidAsset and it will pretty much work like ConfigurationManager with a few additional setup steps.

There is a lot of options out there to try, but I wanted to try something else... How else will I make up content for my blog? I wanted to build something that is:

Build configuration specific,

Strongly typed,

Automatically updates on build, and;

Flexible

1. Build Configuration Specific - SlowCheetah

To make this solution build configuration specific, we are going to make use of transform files. This post explains transform files really well and I've included an extract from the post below.

A transform file is an XML file that specifies how the Web.config file should be changed when it is deployed

...

The purpose of these files is to generate a new Web.config file depending upon the environment. It's extremely useful to store settings in these files...

As you can see, this is very useful indeed. However, they are only mentioning Web.config files, what about other XML files? I've used SlowCheetah in the past to achieve this, and it is a fantastic tool!

It allows you to transform any XML-based file based on your build configuration. E.g. you can create a file called Bob.xml, right-click and choose to add transforms. For this to work from Visual Studio you need to install the extension and whenever you add a transform to an xml file it will add the appropriate build targets to your .csproj file and a NuGet package.

We'll start by adding a text file and renaming it to App.config, and add the following XML with single app setting for the BaseServerUrl:

SlowCheetah will transform the XML file and the resulting file will be copied to project's output directory. SlowCheetah will not update the source file, only the output file. I would like to do that. You can do this by adding a BeforeBuild target to the project file (.csproj):

2. Strongly typed - T4 Templates

I like T4 Templates. Microsoft seems to make good use of it, especially with Entity Framework (*.edmx), and on the Xamarin Android side with the Resources file. The Resources file updates on build, so, I thought I'd try and make it work for our scenario. Let's start by adding a new item to our project and searching for "text template", select the template, and call it CoreSettings.tt:

The T4 template basically creates a new C# file with the same name and reads the appSettings node from the App.config file. When you save the changes to CoreSettings.tt, CoreSettings.cs will update and now you have a strongly typed settings class:

/*
Auto-generated code, do not change. If you really need to change something, for example, you need to do this for Web.config, you
can change the template here: 'C:\Development\Personal\BarelyCompetent.XamarinSettings\src\BarelyCompetent.XamarinSettings.Core\CoreSettings.tt'.
*/
namespace BarelyCompetent.XamarinSettings.Core
{
public class CoreSettings
{
private static CoreSettings _current;
public static CoreSettings Current => _current ?? (_current = new CoreSettings());
public string BaseServerUrl => "http://dev.product/api";
}
}

I am by no means a T4 expert and the above code is probably not the best way of doing it, but it works for my scenario and you can edit it as you wish.

The T4 Template satisfies our second requirement: Strongly typed, but you must find and save the CoreSettings.tt file every time to get the latest update.

3. Automatically updates on build - Clarius.TransformOnBuild

I recently came across a library called Clarius.TransformOnBuild. The library consists of a MSBuild targets file, that will try to find the Transform.exe (this is what T4 files use in the background) file in the environment that you are building from. If it finds the executable, it will traverse your solution files for items with a Build Action of None that can be transformed.

To get this working you can add the NuGet package to your solution.

Install-Package Clarius.TransformOnBuild

And change the Build Action of CoreSettings.tt to None:

If you build your solution now, you will see that the CoreSettings.cs file will be updated. Clarius.TransformOnBuild satisfies our 3rd requirement: Automatically updates on build.

Finally, we can now use consume our CoreSettings.cs file:

CoreSettings.Current.BaseServerUrl

4. Flexible

In terms of flexibility the solution is simple, and you can extend it easily by changing the T4 Template, or adding additional setting files for different projects. This was written with Xamarin Android in mind, but you can apply the same principles for any .Net project.
Please let me know your thoughts in the comments section below.