0

I am looking for a simple and elegant means to store settings for my application. Here is an example that closely depicts that I am looking for

public class Office
{
    string location;
    int numberOfWorkStations;
    int numberOfServers;
    string developerNames[];
}

And the configuration is as below:

<Office>
  <Location>Mumbai, India</Location>
  <NumberOfWorkStations>10</NumberOfWorkStations>
  <NumberOfServers>2</NumberOfServers>
  <DeveloperNames>
      <DeveloperName>GoGo</DeveloperName>
      <DeveloperName>MoMo</DeveloperName>
      <DeveloperName>JoJo</DeveloperName>
  </DeveloperNames>
</Office>

Back in 2005/6 there used to be an Enterprise Library Configuration Block that would abstract all of the XML Serialization stuff.

I am looking at the latest version of the Enterprise Library but seems like the Configuration Block does not exist any more.

I am on .Net framework 4.5 and my thought is that since the feature has been taken off from the enterprise library, it should now exist natively in the .Net framework.

I have read this blog but feel deriving from ConfigurationSection, ConfigurationElement etc. is still too much work compared to what Enterprise Library had offered in the past. What I am looking for is very closer to XMLSerialization, but I don't want to write code to be doing the serialization as I feel this would be like reinventing the wheel.

Thanks for looking up my post.

Moiz Tankiwala
  • 6,070
  • 7
  • 38
  • 51

2 Answers2

3

I use Castle Windsor XML Inline Parameters:

Here is the config file (save it as OfficeConfig.config and put it in the same folder with your exe)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <components>
    <component id="Office">
      <parameters>
        <Location>Mumbai, India</Location>
        <NumberOfWorkStations>10</NumberOfWorkStations>
        <NumberOfServers>2</NumberOfServers>
        <DeveloperNames>
          <array>
            <item>GoGo</item>
            <item>MoMo</item>
            <item>JoJo</item>
          </array>
        </DeveloperNames>
      </parameters>
    </component>
  </components>
</configuration>

And here is the code:

namespace ConsoleApplication1
{
    using System;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    using Castle.Windsor.Installer;

    public class Office
    {
        public string Location { get; set; }
        public int NumberOfWorkStations { get; set; }
        public int NumberOfServers { get; set; }
        public string[] DeveloperNames { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var container = new WindsorContainer()
                  .Install(Configuration.FromXmlFile("OfficeConfig.config"))
                  .Register(
                    Component.For<Office>().Named("Office").LifeStyle.Singleton,
                    Component.For<Program>().LifeStyle.Transient);

            var program = container.Resolve<Program>();
        }

        public Program(Office office)
        {
            Console.WriteLine(office.Location);
            Console.WriteLine(office.NumberOfWorkStations);
            Console.WriteLine(office.NumberOfServers);
            foreach (var name in office.DeveloperNames)
            {
                Console.WriteLine(name);
            }
        }
    }
}

You can even use List and Dictionary as properties

yclkvnc
  • 913
  • 8
  • 15
1

I am looking at the latest version of the Enterprise Library but seems like the Configuration Block does not exist any more.

I am on .Net framework 4.5 and my thought is that since the feature has been taken off from the enterprise library, it should now exist natively in the .Net framework.

Yes, your assumptions are correct. The configuration block was dropped when the functionality made it's way into the .NET Framework.

Another approach would be to leverage a tool to create the configuration classes. For example, Configuration Section Designer.

enter image description here

Randy Levy
  • 22,566
  • 4
  • 68
  • 94