Wednesday, November 11, 2009

Using the configuration section designer

There’s an awesome tool which one of my work mates showed me called the Configuration Section Designer. It’s an open source project and be found on Codeplex here.

It really beats hand cranking a custom configuration and generates a nice API for using your new configuration section.

After installing it, it’s simple to add a new configuration.


This will split out a bunch of generated files, in my case:


The one we are interested in is the .csd file, which is where we go to edit the configuration section using the designer. So let’s open that one up, and we get a nice designer with some logical toolbox items:


I’ve created my custom configuration section with a single attribute called installationRoot:


When you try and save, the CSD attempts to validate your configuration section, and errors will appear in the usual visual studio error box:


It will want you to give the namespaces for where you want the generated files to go, in my case I used their physical location in the solution so everything matches. You can edit namespaces and xml namespaces as well as types for the attributes and everything else in the properties box.


It’s all quite logical and straightforward to use. It really comes in handy when you start putting together complex configuration sections and realise you have no code to write:


Consuming the generated API is also very straightforward:

public void Create_WithValidConfiguration_HydratesStructureConfigurationObject()
Structure structure = NPkgConfiguration.Instance.structure;

Assert.AreEqual("package", structure.packageRoot, "Failed to hydrate correctly.");
Assert.AreEqual("installation", structure.installationRoot, "Failed to hydrate correctly.");
Assert.AreEqual("source", structure.sourceRoot, "Failed to hydrate correctly.");
Assert.AreEqual("document", structure.documentRoot, "Failed to hydrate correctly.");

Hope you enjoy using it!

No comments:

Post a Comment