Tue, Apr 17 2007 4:53 PM
Olaf Conijn
EntLib Configuration Console: Have it your way!
As you might already know, one of the additions to Enterprise Library version 3 is the ability to manage configuration files from within the Visual Studio IDE.
In order to have both the existing configuration tool and the integrated editor for Visual Studio use the same logic under the covers EntLib 3 also introduces a new assembly called Microsoft.Practices.EnterpriseLibrary.Configuration.Design.UI. This assembly contains the functionality that is shared between the 2 tools.
Since this Configuration.Design.UI is contained in the package, let's have a look at the effort it takes to create your own user interface that leverages the EntLibs designtime nodes :-). I have to add that this never was a scenario for EntLib 3, again we might be abusing it in ways it is unanticipated for *evil grin*.
Now, why would this be usefull? well maybe you work with a company that uses EntLib inside their own framework and want a branded version of this tooling. I could also see this work inside installers (after deploying a website or service you might want to configure the application, right?)
So, let's start out with creating a winforms project. Add a panel (1), list-view (2) and property grid (3). Then add all the buttons and branding you feel is vital to this applications success (4) :-)

Next, let's add some references to this project. First add the:
Microsoft.Practices.EnterpriseLibrary.Configuration.Design.HostAdapter,
Microsoft.Practices.EnterpriseLibrary.Configuration.Design.UI and
Microsoft.Practices.EnterpriseLibrary.Configuration.Design
This should get us started with all of the generic designtime features EntLib provides. You should also add references to the designtime of each block you'd like to configure using this version of the tool. This means adding references to the <block> and <block.configuration.design> assemblies (or manually copy these assemblies into the \bin\ directory, since we wont be coding against those).
For example, to have this tool manage Validation configuration, add:
Microsoft.Practices.EnterpriseLibrary.Validation and
Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.Design
I added all the assemblies from EntLibs bin-directory as a reference, that should give you all the designtime features the other tools have as well.
Since we have the references, we can start coding!
first, lets add a field of type SingleHierarchyConfigurationUIHostAdapater to our form. And initialize it in our constructor. The initialization code will instantiate our "ConfigurationHostAdapter", add the control it exposes to our form (inside the Panel we designed) and wire a couple of events
public partial class Form1 : Form
{
SingleHierarchyConfigurationUIHostAdapater configurationConsoleAdapter;
public Form1()
{
InitializeComponent();
HostAdapaterConfiguration adapterConfig = new HostAdapaterConfiguration(AppDomain.CurrentDomain.BaseDirectory);
configurationConsoleAdapter = new SingleHierarchyConfigurationUIHostAdapater(adapterConfig);
Panel1.Controls.Add(configurationConsoleAdapter.EditorControl);
configurationConsoleAdapter.EditorControl.Dock = DockStyle.Fill;
configurationConsoleAdapter.SelectionChanged += new EventHandler<SelectionChangedEventArgs>(configurationConsoleAdapter_SelectionChanged);
configurationConsoleAdapter.TasksChanged += new EventHandler<TasksChangedEventArgs>(configurationConsoleAdapter_TasksChanged);
configurationConsoleAdapter.DocumentClosed += new EventHandler<EventArgs>(configurationConsoleAdapter_DocumentClosed);
}
Cool, now for the code we want to execute on these events (I assume most of this is self-explanetory):
void configurationConsoleAdapter_DocumentClosed(object sender, EventArgs e)
{
Close();
}
void configurationConsoleAdapter_TasksChanged(object sender, TasksChangedEventArgs e)
{
listView1.Items.Clear();
foreach (Task t in e.Tasks)
{
listView1.Items.Add(new TaskViewItem(t));
}
}
void configurationConsoleAdapter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
propertyGrid1.SelectedObject = e.SelectedComponent;
}
private class TaskViewItem : ListViewItem
{
public TaskViewItem(Task t): base(t.Message)
{
Tag = t;
}
}
Then, last -but not least - lets write the handlers that execute whenever the toolstrip-buttons "Open", "Save" or "Validate" are clicked:
private void openButton_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
configurationConsoleAdapter.Load(dialog.FileName);
}
}
}
private void saveButton_Click(object sender, EventArgs e)
{
using (SaveFileDialog dialog = new SaveFileDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
configurationConsoleAdapter.Save(dialog.FileName);
}
}
MessageBox.Show("Save completed");
}
private void validateButton_Click(object sender, EventArgs e)
{
configurationConsoleAdapter.Validate();
MessageBox.Show("validate completed");
}
Et voila, the result after this 15 minute excercise:

Then, order a couple of hundred thumb-drives. copy the tool on them and distribute them to your companies consultants for that unique corporate feel your company has! :-)
Attached you can find the configuration-tool in the screenshot.
Filed under: C#, Contains code, Contains downloads, Enterprise Library