Git Product home page Git Product logo

configplugin's Introduction

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:

  • Configuration class and json files must be in the folder {PCL_Project}\Config.
  • Json files must be configured with Build Action Embedded Resource.

Possible exceptions:

  • You must call the Init method before using the Configuration. You need to initialize the ** ConfigurationManager ** component when the application starts.
  • Configuration class not found. You must validate that the Configuration class has been implemented and is located in {PCL_Project}\Config.
  • Configuration file not found ({0}). Verify that the configuration file has the Build Action property in Embedded Resource. You must validate that the json files are configured with Build Action Embedded Resource and that their names correspond to those in the Configuration class with the build directives.

Improvements to implement:

  • In the nuget installation, the json files are automatically configured with the BuildAction property in Embedded Resource.

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.

configplugin's People

Contributors

agustinbonilla avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.