Home

Awesome

ConfigPlugin for Xamarin Applications

ConfigPlugin Logo

Config plugin for Xamarin.Forms, Xamarin.Android and Xamarin.iOS applications.

This plugin allows to define the settings of the application for each environment in which it will run. This plugin uses json files to maintain the settings, so a file will be configured for each environment.

For example: three json files are configured, one with the development environment settings, another with the testing environment settings and finally one with the production environment settings.

Setup

API Usage

Initialize the ConfigurationManager Initialize the ConfigurationManager component when the application starts.

public App()
{
    InitializeComponent();

    // Initialize the ConfigurationManager component when the application starts.
    ConfigurationManager.Init(this);

    MainPage = new ConfigPluginSample.MainPage();
}

Implement Configuration class CurrentConfiguration must be implemented, using build directives to link the json files for each enviroment. Create a property for each application configuration.

public class Configuration : IConfiguration
{
    public string CurrentConfiguration
    {
        get
        {
#if __PROD
            return "Prod";
#elif __TEST
            return "Test";
#else
            return "Dev";
#endif
        }
    }

    // Create a property for each application configuration.

    public string UrlWebService { get; set; }

    public string CloudDataBaseConnectionString { get; set; }

    public string PushNotificationService { get; set; }

    public string UrlAnalyticsService { get; set; }
}

Create configurations in json files The configurations must be created in each of the json files, the properties defined in the Configuration class must be exactly the same as those defined in the json files.

Dev.json

{
  "UrlWebService": "https://myservicedev.azurewebsites.net",
  "CloudDataBaseConnectionString": "myserverdev.database.windows.net",
  "PushNotificationService": "notificationhubdev.servicebus.windows.net",
  "UrlAnalyticsService": "https://mobile.azure.com/apps/appdev"
}

Test.json

{
  "UrlWebService": "https://myservicetest.azurewebsites.net",
  "CloudDataBaseConnectionString": "myservertest.database.windows.net",
  "PushNotificationService": "notificationhubtest.servicebus.windows.net",
  "UrlAnalyticsService": "https://mobile.azure.com/apps/apptest"
}

Prod.json

{
  "UrlWebService": "https://myserviceprod.azurewebsites.net",
  "CloudDataBaseConnectionString": "myserverprod.database.windows.net",
  "PushNotificationService": "notificationhubprod.servicebus.windows.net",
  "UrlAnalyticsService": "https://mobile.azure.com/apps/appprod"
}

Access to Configurations Get the configurations defined for the running environment.

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        // Get the configurations defined for the running environment.

        lblUrlWebService.Text = ((Configuration)ConfigurationManager.Configuration).UrlWebService;

        lblCloudDataBaseConnectionString.Text = ((Configuration)ConfigurationManager.Configuration).CloudDataBaseConnectionString;

        lblPushNotificationService.Text = ((Configuration)ConfigurationManager.Configuration).PushNotificationService;

        lblUrlAnalyticsService.Text = ((Configuration)ConfigurationManager.Configuration).UrlAnalyticsService;
    }
}

Important:

Possible exceptions:

Improvements to implement:

Contributions

Contributions are welcome! If you find a bug please report it and if you want a feature please report it. If you want to contribute code please file an issue and create a branch off of the current develop branch and file a pull request.