IoC - 构造函数将运行时值作为一个参数,将服务作为另一个参数

发布于 2024-10-25 22:27:09 字数 678 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自我难过 2024-11-01 22:27:09

永远记住,在应用程序代码中,从容器中拉取永远不是解决方案。 应用程序代码应该不知道有一个 DI 容器正在运行

一般解决方案您需要根据运行时值解析依赖关系,方法是使用抽象工厂

在您的情况下,工厂可能如下所示(假设您的 config 变量是字符串:

public interface IViewModelFactory
{
    IViewModel Create(string configuration);
}

现在您可以将 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:

public interface IViewModelFactory
{
    IViewModel Create(string configuration);
}

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.

笑叹一世浮沉 2024-11-01 22:27:09

您可以通过使用以 IDictionary 作为参数的 IWindsorContainer.Resolve 重载,将参数传递给 Windsor,以便在解析构造函数时使用这些参数。在这个字典中,键应该是参数名称,值应该是用作参数值的对象:

var arguments = new Dictionary<string,object> {{ "config", config }, { "service", svc } };
var viewModel = container.Resolve<MyViewModel>(arguments);

You can pass parameters to Windsor, that it should use when resolving the constructor, by using the overload of IWindsorContainer.Resolve that takes an IDictionary 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:

var arguments = new Dictionary<string,object> {{ "config", config }, { "service", svc } };
var viewModel = container.Resolve<MyViewModel>(arguments);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文