Created
September 27, 2011 18:00
-
-
Save PaulStovell/1245764 to your computer and use it in GitHub Desktop.
Octopus configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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