Monday, March 7, 2011

create your own settings in xml

I'm in a ASP.NET project where I need to give several parameters to the administrator that is going to install the website, like:

AllowUserToChangePanelLayout
AllowUserToDeleteCompany

etc...

My question is, will be a good thing to add this into the web.config file, using my own configSession or add as a profile varibles? or should I create a XML file for this?

What do you do and what are the cons and favs?

I originally thought about web.config but I then realized that I should mess up with Website configurations and my own web app configuration and that I should create a different file, them I read this post and now I'm on this place... should I do this or that?

From stackoverflow
  • I usually use Settings - available via the project properties - Settings. These can be edited and saved in code, and I write a form / web page to edit them.

    If you want to use the XML configuration, there's an attribute called file that reads external files. You could have a web.config file and a someothername.config file. The someothername.config would have settings like:

    <appSettings>
        <add key="ConnString" value="my conn string" />
        <add key="MaxUsers" value="50" />
    </appSettings>
    

    And the web.config would have

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <appSettings file="ExternalWeb.config">
            <add key="MyKey" value="MyValue" />
        </appSettings>
    </configuration>
    

    See DevX for the example I stole.

    balexandre : excellent idea :) I just added, tests it and works like a charm! Thanks
    : Be advised that the external file is cached. If you make changes to it, you'll have to restart the application to reload settings.
  • just to let you guys know that I did what configurator recommended but with a twist.

    instead of asking all the time (that I need) for

    System.Configuration.ConfigurationManager.AppSettings["myKey"];
    

    I just created a static class that would pull this values with what we call by Strongly typed values (so you don't need to remember all the values)

    the mySettings class

    public static class mySettings
    {
        public enum SettingsType
        { UserPermitions, WebService, Alerts }
        public enum SectionType
        { AllowChangeLayout, AllowUserDelete, MaximumReturnsFromSearch, MaximumOnBatch, SendTo }
    
        public static String GetSettings(SettingsType type, SectionType section)
        {
            return
                ConfigurationManager.AppSettings[
                    String.Format("{0}_{1}",
                        Enum.Parse(typeof(SettingsType), type.ToString()).ToString(),
                        Enum.Parse(typeof(SectionType), section.ToString()).ToString())
                ];
        }
    }
    

    the web.config appSettings part

    <configuration>
      <appSettings file="myApp.config">
        <add key="UserPermitions_AllowChangeLayout" value="" />
        <add key="UserPermitions_AllowUserDelete" value="" />    
    
        <add key="WebService_MaximumReturnsFromSearch" value="" /> 
    
        <add key="Alerts_SendTo" value="" />
        <add key="Alerts_MaximumOnBatch" value="" />
      </appSettings>
    </configuration>
    

    the entire myApp.config file

    <?xml version="1.0" encoding="utf-8" ?>
    <!--
    ###
    ### This file serves the propose of a quick configuration.
    ### Administrator can either change this values directly or use the 
    ###   Settings tab in the application.
    ###
    -->
    <appSettings>
    
      <!-- *** User Access Configuration *** -->
      <!-- Allow user to change the panels layout {1: Yes} {0: No} -->
      <add key="UserPermitions_AllowChangeLayout" value="1" />
      <!-- Allow user to delete a company fro monitoring -->
      <add key="UserPermitions_AllowUserDelete" value="1" />
    
      <!-- *** Web Service configuration *** -->
      <!-- Maximum responses from the search service -->
      <add key="WebService_MaximumReturnsFromSearch" value="10" />
    
      <!-- *** Allerts configuration *** -->
      <!-- Send the alerts to the email writeen below -->
      <add key="Alerts_SendTo" value="bruno.in.dk@gmail.com" />
      <!-- Send an alert when user import more than the number bellow -->
      <add key="Alerts_MaximumOnBatch" value="10" />
    
    </appSettings>
    

    So, now I call like this:

    p.value = mySettings.GetSettings(
                 mySettings.SettingsType.WebService, 
                 mySettings.SectionType.MaximumReturnsFromSearch);
    

    Hope that helps someone with the same problem :)

    : This doesn't look very scalable because you'll have to keep the enums updated all the time... if you want named properties, a .settings resource file is probably more practical. It's not maintainable from outside the app, but you could use it as a strongly typed map for the .config key strings.
    : By the way (offtopic), isn't it supposed to be "permissions" instead of "permitions"?

0 comments:

Post a Comment