抽象基类和应用程序域

发布于 2024-08-13 05:48:15 字数 376 浏览 2 评论 0原文

如果我接下来的解释没有足够的意义,我现在道歉;我因此而闻名,尽管我尝试着做其他事情。

我正在编写一项使用用户定义插件的服务。我尝试通过使用共享程序集中定义的接口来隔离它们 - 将它们的程序集排除在服务的应用程序域之外。

让我烦恼的是抽象基类的使用。有些功能对于某些接口的所有实现来说都是通用的,因此抽象基类是有意义的。如果抽象基础位于服务程序集中,那么无论它的子类化插件如何将其程序集拖到服务的应用程序域中。但是,服务使用的抽象基(具有内部 setter 和公共 getter 的属性)中有内部成员,因此它需要与服务位于同一程序集中才能实现这一点。

似乎我想要的东西是不可能的,但我也相信这是因为我采取了错误的方法。我拼命地尝试在这次练习和学习中更好地利用好的模式和实践。

I apologize now if my upcoming explanation doesn't make enough sense; I'm reknown for it, though I try to do otherwise.

I'm writing a service that makes use of user-defined plugins. I'm trying to isolate them -- keeping their assemblies out of the service's appdomain -- by making use of interfaces defined in a shared assembly.

What's killing me is the use of abstract base classes. There is functionality that will be common to all implementations of some of the interfaces, so abstract base classes make sense. If an abstract base is in the service assembly, then whatever plugins that subclass it get their assemblies dragged into the service's appdomain. However, there are internal members in the abstract base (properties with internal setters and public getters) that the service uses so it needs to be in the same assembly as the service for that to be possible.

It seems like what I want is not possible, but I also believe that it's because I'm taking the wrong approach. I'm desperately trying to make better use of good patterns and practices in this exercise and learning along the way.

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

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

发布评论

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

评论(3

墨落画卷 2024-08-20 05:48:15

您可能想要的是一个带有抽象基类的接口,该基类实现该接口并且派生类可以继承。在这种情况下,您可以保持与接口的分离,但仍然提供用于实现的抽象基类。这还有一个优点是抽象基类是可选的。

What you probably want is an interface with an abstract base class that implements the interface and derived classes can inherit from. In this case you can maintain your separation with the interface but still provide the abstract base class for implementation. This also has the advantage that the abstract base class is optional.

久隐师 2024-08-20 05:48:15

如果您试图避免的问题是将插件程序集泄漏到您的服务 AppDomain 中,那么无论您是否有内部成员,您都不会遇到该问题。您只需要服务程序集在插件域内可用(而不是相反),并且您可能必须在服务程序集中定义共享类型,而不是单独的程序集(如果您确实需要的“内部”)另一个组件)。

假设您在 ServiceLib.dll 中定义了抽象基类 PluginBase。然后,您可以在主服务 AppDomain 中包含这样的代码:

// Create a new AppDomain for the plugin
AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain", null, new AppDomainSetup());

// Instantiate the plugin type (in the new AppDomain)
// Note: assumes that PluginBase is MarshalByRefObject
PluginBase plugin = (PluginBase)domain.CreateInstanceAndUnwrap("PluginLib", "PluginLib.PluginImp");

// Set any internal stuff now
plugin.InternalDetails = "...";

If the problem you are trying to avoid is leaking the plug-in assembly into your service AppDomain, then you won't have that problem whether you have internal members or not. You will simply need the service assembly to be available inside the plugin domain (not the other way around) and you'll probably have to define the shared type in the service assembly rather than a separate assembly (if you indeed need "internals" of the other assembly).

Imagine you have abstract base class PluginBase defined in ServiceLib.dll. You could then have code like this in your main service AppDomain:

// Create a new AppDomain for the plugin
AppDomain pluginDomain = AppDomain.CreateDomain("PluginDomain", null, new AppDomainSetup());

// Instantiate the plugin type (in the new AppDomain)
// Note: assumes that PluginBase is MarshalByRefObject
PluginBase plugin = (PluginBase)domain.CreateInstanceAndUnwrap("PluginLib", "PluginLib.PluginImp");

// Set any internal stuff now
plugin.InternalDetails = "...";
往昔成烟 2024-08-20 05:48:15

(供我和其他人将来参考)

事实证明,这是一个毫无意义的练习。

一旦插件创建的对象的代理在其他应用程序域中可用,该服务定义的基类中以内部方式使用服务的任何内容(例如针对集合的查找),它就会针对以下应用程序域中的服务对象的副本进行操作插件的应用程序域,而不是我试图提供的单例。

我想我要么放弃我的多应用程序域追求,要么放弃在内部做任何事情。如果没有内部操作,基类可以从服务中分离出来,但它必须像其他所有东西一样与服务交互。

我确实喜欢学习,但我不喜欢一路上的坎坷。

(For my and possibly others' future reference)

It turns out that this was a somewhat pointless exercise.

Once the plugin-created object's proxy was available in other appdomains, anything in that service-defined base class that makes use of the service in an internal fashion (like a lookup against a collection), it operates against the copy of the service object in the plugin's appdomain, not the singleton I'm trying to provide.

I think I'm down to either abandoning my multiple-appdomain quest or abandoning doing anything internally. If there are no internal operations, the base class can be split from the service, but then it has to interact with the service just like everything else does.

I do enjoy learning, but I do not appreciate the bumps on the head along the way.

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