IoC - 构造函数将运行时值作为一个参数,将服务作为另一个参数
我有一个 WPF 应用程序,当它启动时,它会在文件系统中查找一些配置文件
对于它找到的每个配置文件,它会在不同的窗口中显示一些信息
每个窗口都有一个关联的 ViewModel 对象,该对象绑定到 windows datacontext
所以为每个配置文件创建一个新的 ViewModel。表示配置文件中数据的对象被传递到 viewmodels 构造函数中
但是,视图模型还具有传递到构造函数中的其他依赖项
代码看起来像这样(在从 app.xaml 启动的引导程序中)
foreach (WindowConfig config in ConfigManager.GetConfigs())
{
IMyService svc = new MyService();
//change to resolve from IoC container
MyViewModel vm = new MyViewModel(config, svc);
Window1 view = new Window1();
view.DataContext = vm;
window.show();
}
我想使用 Castle IoC容器解决了这些依赖性。我知道如何为 IMyService 执行此操作,但是如何为从配置文件创建的特定类执行此操作?
谢谢
I have a WPF app which, when it starts, looks at the file system for some config files
For each config file it finds, it displays some info in a different window
Each window has an associated ViewModel object which is bound to the windows datacontext
So a new ViewModel is created for each config file. An object representing the data in the config file is passed into the viewmodels constructor
However, the View model also has other dependancies passed into the constructor
The code looks something like this (in a bootstrapper initiated from app.xaml)
foreach (WindowConfig config in ConfigManager.GetConfigs())
{
IMyService svc = new MyService();
//change to resolve from IoC container
MyViewModel vm = new MyViewModel(config, svc);
Window1 view = new Window1();
view.DataContext = vm;
window.show();
}
I want to use Castle IoC contaoiner resolve these dependancies. I know how to do that for IMyService, but how can I do it for the specific class that has been created from the config file ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
永远记住,在应用程序代码中,从容器中拉取永远不是解决方案。 应用程序代码应该不知道有一个 DI 容器正在运行。
一般解决方案您需要根据运行时值解析依赖关系,方法是使用抽象工厂。
在您的情况下,工厂可能如下所示(假设您的 config 变量是字符串:
现在您可以将 IViewModelFactory 作为单个依赖项注入到循环配置文件的类中。
要实现 IViewModelFactory,您可以可以手动完成,也可以使用温莎城堡的 类型化工厂为您实现它的设施。
Always remember that in the application code, pulling from the container is never the solution. Application code should be unaware that there's a DI container in play.
The general solution when you need to resolve a dependency based on a run-time value is to use an Abstract Factory.
In your case, the factory might look like this (assuming that your
config
variables are strings:Now you can inject the IViewModelFactory as a single dependency into the class that loops through the configuration files.
To implement IViewModelFactory you can either do it by hand or use Castle Windsor's Typed Factory Facility to implement it for you.
您可以通过使用以
IDictionary
作为参数的IWindsorContainer.Resolve
重载,将参数传递给 Windsor,以便在解析构造函数时使用这些参数。在这个字典中,键应该是参数名称,值应该是用作参数值的对象:You can pass parameters to Windsor, that it should use when resolving the constructor, by using the overload of
IWindsorContainer.Resolve
that takes anIDictionary
as a parameter. In this dictionary, the key should be the parameter name, and the value should be the object to use as the parameter value: