寻找有关处理依赖关系的建议 [英] Looking for some advice on handling dependencies

查看:64
本文介绍了寻找有关处理依赖关系的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种具有模块化体系结构的.Net应用程序,使其可以加载一个或多个插件"程序集.每个程序集都包含一个实现IPlugin接口的类,该接口为托管应用提供了与插件进行交互的各种方法.

托管应用使用

I''m developing a .Net app with a modular architecture allowing it to load one or more "plugin" assemblies. Each assembly contains a class implementing an IPlugin interface which provides various methods for the hosting app to interact with the plugin.

The hosting app registers all these IPlugin classes via Windsor using the

container.Install(FromAssembly...

表达式通过Windsor注册所有这些IPlugin类,然后实例化该类以将插件加载到主应用窗口中.

IPlugin类通常将依赖于其插件中的许多其他类.目前,我正在向Windsor进行注册-托管应用在每个插件中调用IWindsorContainer,允许插件注册此类.然后,通过其构造函数注入IPlugin类的依赖项.

显然,这不是一个好的设计,因为IPlugin类不应从使用中的应用程序中请求此类类(该插件内部的类).同样,这意味着必须公开这些类(或至少它们的构造函数),否则它们将无法在Windsor中注册.

我现在想在内部建立此类类,但是如果这样做,我不确定IPlugin类(它将是插件中唯一的公共类)如何获取其依赖项.我可以看到的选项是:-

1.允许类实例化其自己的依赖关系,但这有悖于良好的OO设计-不能松散耦合,不可测试等.
2.使用服务定位器实例化其自身的依赖关系,但这也令人厌烦.
3.继续使用温莎城堡,确保所有类都是内部的,并带有公共构造函数.这感觉有点棘手,仍然意味着我的IPlugin类将具有一个构造函数,要求注入所有这些类型.

expression. It then instantiates the class as required to load the plugin into the main app window.

An IPlugin class will typically have dependencies on many other classes within its plugin. Currently I''m registering these with Windsor - the hosting app calls an IWindsorContainer in each plugin, allowing the plugin to register such classes. The IPlugin class''s dependencies are then injected via its constructor.

Clearly this isn''t a good design, as the IPlugin class shouldn''t be requesting such classes (which are internal to that plugin) from a consuming application. Also, it means having to make these classes (or at least their constructors) public, otherwise they can''t be registered with Windsor.

I now want to make such classes internal, but if I do this I''m not sure how the IPlugin class (which will be the only public class in the plugin) can get its dependencies. The options I can see are:-

1. Allow the class to instantiate its own dependencies, but this goes against good OO design - not loosely coupled, not testable, etc.
2. Instantiate its own dependencies using a service locator, but that''s also frowned upon.
3. Continue to use Castle Windsor, ensuring that all classes are internal and with a public constructor. This feels a bit hacky, and still means my IPlugin class will have a constructor requiring all these types to be injected.

Any advice or suggestions much appreciated!

推荐答案

这不是一个完整的解决方案,但是如果您还没有的话,请同时阅读两个nopCommerce [ ^ ]和主要 Umbraco [ ^ ],它们都实现了插件建筑.

nopCommerce的模式基于Umbraco,这是一个非常完善的CMS-因此它可能会给您一些好主意.
This isn''t a full solution, but if you haven''t already then have a look at both nopCommerce [^]and mainly Umbraco[^], which both implement a plugin architecture.

nopCommerce based it''s pattern on Umbraco, which is a pretty well established CMS - so it might give you some good ideas.


这篇关于寻找有关处理依赖关系的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆