Created
November 7, 2012 22:36
-
-
Save kkozmic/4034987 to your computer and use it in GitHub Desktop.
Windsor automatic dependencies from appSettings section of app.config
This file contains hidden or 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
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