Skip to content

Configuration

Configuration can be used without any other Shiny module. It does not require the base Shiny hooks in order to function

Features

  • All the power of IConfiguration
  • Load JSON from packaged sources (whitelabellers can unpack, edit, & repack the config)
  • Preferences based configuration source with writebacks
  • Environment variable based file name lookups (ie. appsettings.apple.debug.json)

The Problem

Microsoft really did create a great set of abstractions for configuration. You can store a set of key/values, cause a reload when a configuration source changes, & bind to strongly typed objects using Microsoft.Extensions.Configuration.Binder. On Mobile though, NONE of the current providers aren’t really well.

Why?

  • A big feature for IConfiguration, is the ability to trigger a reload of it’s source without restarting the application. Changing an appsettings.json file during runtime causes the IConfiguration to trigger a reload notification.
  • Mobile doesn’t really have an appsettings.json. Sure you could put this in an embedded resource, but than it is readonly at all times other than the build process… after that - it is locked in place
  • Essentially, IConfiguration becomes a string based dictionary for all intents and purposes - pretty useless considering

The Solution

How Shiny.Extensions.Configuration brings the power of IConfiguration to Mobile!

  • A platform preferences configuration source which allows you to WRITE a value back using IConfiguration[key] = value;
  • A whitelabellers dream - the ability to unpack, change a json config file, and repack without trigger a build by using proper platform directories to load up the JSON files while still having the power of a proper configuration library internally.

Setup

AppSettings JSON

  1. Install the NuGet package

  2. Create an appsettings.json file like you would in ASP.NET Core application in your HEAD project.

  3. Configure your IConfiguration using the following code in your application startup code (ie. Xamarin.Forms App).

    // store this in your dependency injection container OR static class
    var config = new ConfigurationBuilder()
    .AddJsonPlatformBundle("DEBUG") // NOTE: you can change the name of appsettings.json to something else and pass as an argument here
    .Build();
  1. Lastly, appsettings.json also automatically looks for the platform specific appsettings file and overrides/merges the values with the rest of the IConfiguration set.

    The [platform] can be ios, maccatalyst, apple (both ios & maccatalyst), or android This is the general naming convention we follow is appsettings.[platform].[environment].json

    The priority of files in the merge for iOS/Catalyst are:

    • appsettings.ios.[environment].json (or maccatalyst)
    • appsettings.ios.json (or maccatalyst)
    • appsettings.apple.[environment].json
    • appsettings.apple.json
    • appsettings.[platform].json
    • appsettings.json

    And for Android:

    • appsettings.android.[environment].json
    • appsettings.android.json
    • appsettings.json

    If the file(s) don’t exist, they are simply skipped

Preferences Provider

This provider allows writes and persists across application restarts. It is a wrapper around the Android/iOS shared preferences.

  1. Install the NuGet package

  2. For this configuration source, nothing special is required, simply add the following to the configuration builder:

    var configuration = new ConfigurationBuilder()
    .AddPlatformPreferences()
    .Build();
    IConfiguration config = ...; // build
    config["key"] = "value"; // write - this will cause a persist to the platform prefs and also trigger Option reloated events