将依赖项注入 IWindsorInstaller 实现

发布于 2024-12-27 15:02:43 字数 961 浏览 2 评论 0原文

我有一个场景,我想从自定义数据存储中读取控制反转容器(温莎城堡)的配置数据。该自定义数据存储具有类似表格的结构,其中列出了一组(可能是定制的)组件,这些组件实现了系统中所需的接口。

例如,应用程序需要 IFoo 的实现,但包含该实现的实际程序集和类型在此自定义数据存储中定义。

我想要一个 IWindsorInstaller 的实现,它读取数据结构并注册 IFoo 的正确实现者。但是,为了正确访问我的自定义数据存储,我需要访问工厂类(实现 IDataConnectionProvider 接口)。这种类型已在容器中注册,但我不知道这样做是否合理:

var container = new WindsorContainer();
container.Register(Component.For<IDataConnectionProvider>.
               ImplementedBy<DefaultDataConnectionProvider>.
               DependsOn(new { host, service });
container.Install(FromAssembly.This());

然后在程序集中有一个类:

public class PluginInstaller : IWindsorInstaller
{
   public void Install(IWindsorContainer container, IConfigurationStore store)
   {
     var dataStoreConnection = container.Resolve<IDataConnectionProvider>();
     // read table, register other services    
   }
}

这是执行我想做的事情的最佳方法吗?其他建议?

I have a scenario where I would like to read configuration data for an inversion of control container (Castle Windsor) from a custom data store. This custom data store has a table-like structure that lists a set of (potentially bespoke) components that implement required interfaces in the system.

For example, the application requires an implementation of IFoo, but the actual assembly and type containing that implementation is defined in this custom data store.

I'd like to have an implementation of IWindsorInstaller that reads the data structure and registers the correct implementor of IFoo. However, to correctly access my custom data store I need access to a factory class (implementing a IDataConnectionProvider interface). This type is registered in the container, but I don't know if it is reasonable to do something like:

var container = new WindsorContainer();
container.Register(Component.For<IDataConnectionProvider>.
               ImplementedBy<DefaultDataConnectionProvider>.
               DependsOn(new { host, service });
container.Install(FromAssembly.This());

And then have a class in the assembly:

public class PluginInstaller : IWindsorInstaller
{
   public void Install(IWindsorContainer container, IConfigurationStore store)
   {
     var dataStoreConnection = container.Resolve<IDataConnectionProvider>();
     // read table, register other services    
   }
}

Is that the best way to do what I'm trying to do? Other suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

玩套路吗 2025-01-03 15:02:43

是的,这可能也是我会做的。正是出于这个原因,插件安装程序中的 container 参数才存在。

您还可以在容器内注册hostservice,并让它自动连接您的默认数据连接提供程序,即不需要DependsOn

Yes, that is probably what I would do too. The container parameter within your plugin installer is there precisely for this reason.

You also can register host and service within the container and have it wire up your default data connection provider automatically, i.e. there's no need for DependsOn.

乖不如嘢 2025-01-03 15:02:43

一般来说,在安装过程完成之前解决任何问题并不是一个好主意。

相反,我不会在容器中注册 IDataConnectionProvider (除非其他某些组件需要这样做),而只是将其传递到安装程序的构造函数中:

container.Install(
   new PluginInstaller(new DefaultDataConnectionProvider(){Host = host},
   FromAssembly.This()
);

请注意,最后一次调用将尝试实例化 PluginInstaller code> 再次,因此避免这种情况的最简单方法是将类设为内部类。

In general this is not a good idea to resolve anything before installation process is finished.

Instead I would not register the IDataConnectionProvider in the container (unless some other components require that) but just pass it into your installer's constructor:

container.Install(
   new PluginInstaller(new DefaultDataConnectionProvider(){Host = host},
   FromAssembly.This()
);

Notice that the last call will try to instantiate the PluginInstaller again, so the simplest way to avoid that is to make the class internal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文