The bigger issue here is that it's not trivial to store a list of values in appSettings
. This question addresses it and the best answer is to create your own section in the settings file, which you then have to access via ConfigurationManager.GetSection()
instead of ConfigurationManager.AppSettings.Get()
, which is what Dependency.OnAppSettingsValue() uses. Looking through the rest of the Dependency
class, it seems there's no built-in way to do this.
However, if it really is just strings that you need, you have at least two options that aren't all that bad (in my opinion).
1. Store the strings in your App.config file using a StringCollection.
This is just a shorter, built-in version of creating your own section in App.config. Use the editor in Visual Studio's Project Properties page to add an application setting of type StringCollection
. This will make your App.config look something like this:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="YourApplication.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<YourApplication.Properties.Settings>
<setting name="SomeStrings" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>one</string>
<string>two</string>
<string>three</string>
</ArrayOfString>
</value>
</setting>
</YourApplication.Properties.Settings>
</applicationSettings>
</configuration>
Then when configuring your component:
// StringCollection only implements IList, so cast, then convert
var someStrings = Properties.Settings.Default.SomeStrings.Cast<string>().ToArray();
container.Register(Component.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.LifestyleTransient()
.DependsOn(Dependency.OnValue<IList<string>>(someStrings)));
2. Store the strings as a delimited list in appSettings
, then manually split them.
This is likely the simpler approach, though assumes you can figure out a delimiter for your strings (which may not always be the case). Add the values to your App.config file:
<configuration>
<appSettings>
<add key="SomeStrings" value="one;two;three;four" />
</appSettings>
</configuration>
Then when configuring your component:
var someStrings = ConfigurationManager.AppSettings["SomeStrings"].Split(';');
container.Register(Component.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.LifestyleTransient()
.DependsOn(Dependency.OnValue<IList<string>>(someStrings)));
In either case, we're just adding a small amount of work on top of Dependency.OnValue
, which is all Dependency.OnAppSettingsValue
does anyways.
I think this answers your questions, but to be explicit:
- Yes, but you're doing the conversion yourself so you can convert to anything you'd like.
- See the linked question and accepted answer, or use a
StringCollection
.
Dependency.OnValue
is the extension (in my opinion), and I don't know of or see any other place where you would do it. However, given the steps above I don't think this is necessary.