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 ‘Shiny.Extensions.Configuration’
  2. Create an appsettings.json file like you would in ASP.NET Core application in your HEAD project.
    • ANDROID: place the file in: Assets. Ensure the build action on the file is set to ‘AndroidAsset’
    • iOS: place the file in the ROOT of your project. Ensure the build action on the file is set to ‘BundleResource’
  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();

MAUI Setup

  1. For MAUI, install the nuget package ”

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 ‘Shiny.Extensions.Configuration’
  2. For this configuration source, nothing special is required, simply add the following to the configuration builder:
var configuration = new ConfigurationBuilder()
    .AddPlatformPreferences()
    .Build();

NOTE: with the platform preferences provider, changes are supported.

IConfiguration config = ...; // build
config["key"] = "value"; // write - this will cause a persist to the platform prefs and also trigger Option reloated events