Friday Sep 25, 2009

Sample: Customizing Spring.NET configuration variable resolution

While I was playing around with Spring.Objects.Factory.Config.VariablePlaceholderConfigurer I came across a scenario that led me to look into implementing my own IVariableSource . Essentially I was using CommandLineArgsVariableSource as the preferred source, trying EnvironmentVariableSource next, and then finally defaulting to a config file. In this case, I wanted the environment variable name to be more specific (qualified with an application identifier) than the command line arg names (keep them short). This is really a generic strategy rewrite the variable name and delegate to an existing implementation.

public class PrefixedEnvironmentVariableSource : IVariableSource
{
// EnvironmentVariableSource to be wrapped by this instance
private EnvironmentVariableSource environmentVariableSource;

/// Prefix to be prepended to config variable names when searching for values in environment.
public string Prefix { get; set; }

public PrefixedEnvironmentVariableSource()
{
environmentVariableSource = new EnvironmentVariableSource();
Prefix = String.Empty;
}

#region IVariableSource Members

public string ResolveVariable(string name)
{
return environmentVariableSource.ResolveVariable(Prefix + name);
}

#endregion
}

Comments:

Post a Comment:
  • HTML Syntax: Allowed