Castle Windsor:如何将 appSettings 依赖项转换为列表或数组?
Castle Windsor: How to convert an appSettings dependency to a list or an array?
我有一个依赖于字符串列表的组件:
// ctor
public MyComponent(IList<string> someStrings) { ... }
使用 Castle Windsor,我试图从我的 app.config 文件的 AppSettings
部分提供这种依赖关系,如下所示:
container.Register(Component
.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.DependsOn(Dependency.OnAppSettingsValue("someStrings", "SomeStrings"))
);
Inline Dependencies 上的温莎文档说:
appSettings and conversion: Values in the config file are stored as text, yet the dependencies in your code may be of other types (TimeSpan
in this example). Windsor has got you covered, and for most cases will perform the appropriate conversion for you.
- 这是否涵盖了我想转换为
IList<string>
的情况?
- 如果是这样,在 app.config 文件中输入字符串列表的正确方法是什么?
- 如果没有,是否有我可以指定自己的转换的扩展点?
这里更大的问题是在 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。查看 Dependency
class 的其余部分,似乎没有内置方法可以做到这一点。
但是,如果它真的只是您需要的字符串,那么您至少有两个还不错的选项(在我看来)。
1.使用 StringCollection 将字符串存储在 App.config 文件中。
这只是在 App.config 中创建您自己的部分的较短的内置版本。使用 Visual Studio 的项目属性页面中的编辑器添加类型 StringCollection
的应用程序设置。这将使您的 App.config 看起来像这样:
<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>
然后在配置组件时:
// 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。将字符串作为分隔列表存储在 appSettings
中,然后手动拆分它们。
这可能是更简单的方法,但假设您可以找出字符串的分隔符(情况可能并非总是如此)。将值添加到您的 App.config 文件:
<configuration>
<appSettings>
<add key="SomeStrings" value="one;two;three;four" />
</appSettings>
</configuration>
然后在配置组件时:
var someStrings = ConfigurationManager.AppSettings["SomeStrings"].Split(';');
container.Register(Component.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.LifestyleTransient()
.DependsOn(Dependency.OnValue<IList<string>>(someStrings)));
在任何一种情况下,我们只是在 Dependency.OnValue
之上添加少量工作,这就是 Dependency.OnAppSettingsValue
所做的全部工作。
我认为这回答了你的问题,但要明确:
- 是的,但是您要自己进行转换,因此您可以转换为您喜欢的任何内容。
- 查看链接问题和 accepted answer,或使用
StringCollection
。
Dependency.OnValue
是扩展名(在我看来),我不知道或看到您会在其他任何地方这样做。但是,鉴于上述步骤,我认为这不是必需的。
我有一个依赖于字符串列表的组件:
// ctor
public MyComponent(IList<string> someStrings) { ... }
使用 Castle Windsor,我试图从我的 app.config 文件的 AppSettings
部分提供这种依赖关系,如下所示:
container.Register(Component
.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.DependsOn(Dependency.OnAppSettingsValue("someStrings", "SomeStrings"))
);
Inline Dependencies 上的温莎文档说:
appSettings and conversion: Values in the config file are stored as text, yet the dependencies in your code may be of other types (
TimeSpan
in this example). Windsor has got you covered, and for most cases will perform the appropriate conversion for you.
- 这是否涵盖了我想转换为
IList<string>
的情况? - 如果是这样,在 app.config 文件中输入字符串列表的正确方法是什么?
- 如果没有,是否有我可以指定自己的转换的扩展点?
这里更大的问题是在 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。查看 Dependency
class 的其余部分,似乎没有内置方法可以做到这一点。
但是,如果它真的只是您需要的字符串,那么您至少有两个还不错的选项(在我看来)。
1.使用 StringCollection 将字符串存储在 App.config 文件中。
这只是在 App.config 中创建您自己的部分的较短的内置版本。使用 Visual Studio 的项目属性页面中的编辑器添加类型 StringCollection
的应用程序设置。这将使您的 App.config 看起来像这样:
<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>
然后在配置组件时:
// 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。将字符串作为分隔列表存储在 appSettings
中,然后手动拆分它们。
这可能是更简单的方法,但假设您可以找出字符串的分隔符(情况可能并非总是如此)。将值添加到您的 App.config 文件:
<configuration>
<appSettings>
<add key="SomeStrings" value="one;two;three;four" />
</appSettings>
</configuration>
然后在配置组件时:
var someStrings = ConfigurationManager.AppSettings["SomeStrings"].Split(';');
container.Register(Component.For<IMyComponent>()
.ImplementedBy<MyComponent>()
.LifestyleTransient()
.DependsOn(Dependency.OnValue<IList<string>>(someStrings)));
在任何一种情况下,我们只是在 Dependency.OnValue
之上添加少量工作,这就是 Dependency.OnAppSettingsValue
所做的全部工作。
我认为这回答了你的问题,但要明确:
- 是的,但是您要自己进行转换,因此您可以转换为您喜欢的任何内容。
- 查看链接问题和 accepted answer,或使用
StringCollection
。 Dependency.OnValue
是扩展名(在我看来),我不知道或看到您会在其他任何地方这样做。但是,鉴于上述步骤,我认为这不是必需的。