Skip to content

seba47/ConfigPlugin

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConfigPlugin for Xamarin Applications

ConfigPlugin Logo

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

This plugin allows you 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, where the build directives must be linked with the names of the json configuration files. 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.

Contributions

Contributions are welcome! If you find a bug please report it and if you want a feature please report it.

About

Config plugin to Xamarin applications.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%