Skip to content

Instantly share code, notes, and snippets.

@kkozmic
Created November 7, 2012 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkozmic/4034987 to your computer and use it in GitHub Desktop.
Save kkozmic/4034987 to your computer and use it in GitHub Desktop.
Windsor automatic dependencies from appSettings section of app.config
public class AppConfigDependencies : IContributeComponentModelConstruction
{
private readonly ParameterModelCollection parameters;
public AppConfigDependencies(NameValueCollection appSettings)
{
parameters = new ParameterModelCollection();
foreach (var key in appSettings.AllKeys)
{
parameters.Add(key, appSettings[key]);
}
}
public void ProcessModel(IKernel kernel, ComponentModel model)
{
InspectProperties(model);
InspectConstructors(model);
}
private void InspectConstructors(ComponentModel model)
{
foreach (var constructor in model.Constructors)
{
foreach (var dependency in constructor.Dependencies)
{
InitDependency(dependency);
}
}
}
private void InspectProperties(ComponentModel model)
{
foreach (var property in model.Properties)
{
InitDependency(property.Dependency);
}
}
private void InitDependency(DependencyModel dependency)
{
// we only do this if the dependency has no associated parameter yet
// to avoid overwriting a value set explicitly
if (dependency.Parameter == null)
{
dependency.Init(parameters);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment