Skip to content

Instantly share code, notes, and snippets.

@PaulStovell
Created September 27, 2011 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulStovell/1245764 to your computer and use it in GitHub Desktop.
Save PaulStovell/1245764 to your computer and use it in GitHub Desktop.
Octopus configuration
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Autofac;
namespace Octopus.Shared.Startup
{
public class ConfiguredModules : Module
{
readonly IList<Module> modules;
public ConfiguredModules(IList<Module> modules)
{
this.modules = modules;
}
protected override void Load(ContainerBuilder builder)
{
var settings = ConfigurationManager.AppSettings;
var keys = settings.AllKeys;
foreach (var setting in keys)
{
if (string.IsNullOrEmpty(setting))
continue;
var parts = setting.Split('.');
if (parts.Length != 2)
continue;
var moduleName = parts[0];
var propertyName = parts[1];
var value = settings[setting];
var module = modules.FirstOrDefault(x => x.GetType().Name == moduleName + "Module");
if (module == null)
continue;
var property = module.GetType().GetProperty(propertyName);
if (property == null)
{
throw new ConfigurationException(string.Format("Invalid configuration key: {0}", setting));
}
property.SetValue(module, Convert.ChangeType(value, property.PropertyType), null);
}
foreach (var module in modules)
{
builder.RegisterModule(module);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment