Search code examples
.netportable-class-library

Config values in Portable Class Library


I'm developing a PCL that will be used by Windows Store and WP8 apps. This library requires some configuration e.g. remote service url. I wanted to put those into the app.config and retrieve them using ConfigurationManager, but the System.Configuration doesn't seem to be available in PCL.


Solution

  • I'd create a configuration class. In the simplest case1 it could look like:

    public class MyLibraryConfig
    {
        public string RemoteServiceUrl{get;set;}
    }
    

    Then pass an instance of this class to the library via standard dependency injection techniques. For example pass it into the constructor and store it in a field. Then it's the application's responsibility to read the configuration from a file, the ConfigurationManager, etc.

    IMO this is much better design and I'd use it over querying the configuration manager even in libraries where I could access the ConfigurationManager. Else you force the application to use a single configuration and to use a specific configuration mechanism.


    1 you can also use an interface or an immutable class. That's slightly more complex, but IMO better design.