围绕第 3 方程序集创建包装器 - 交换和解耦

发布于 2024-08-11 04:12:04 字数 553 浏览 3 评论 0原文

我有一个电子邮件组件,正在集成到我的应用程序中,寻找一些关于如何围绕它构建包装器的提示,以便我可以在需要时将其与另一个第三方组件交换。

我现在的方法是:

  1. 构建一个包含我需要的功能的界面。
  2. 创建一个实现该接口的类,在该类中使用我的第 3 方组件。
  3. 该组件的任何使用都将通过接口进行,如下所示:

    IPop3 pop3 = new AcmeIncePop3Wrapper(); pop3.connect();

在 AcmeIncePop3Wrapper 内部将是:

   public void connect()
   {
         AcmeIncePop3 pop = new AcmeIncePop3();
         pop.connect();
   }

这是一个好方法吗?

我可能可以使用 ninject 添加另一个抽象,这样我就可以交换实现,但实际上这似乎就是我所需要的,因为我不希望每天都更改第 3 方程序集,只是不想让事情变得如此紧密耦合。

I have an email component that I am integrating into my application, looking for some tips on how should build a wrapper around it so I can swap it out with another 3rd party component if needed.

My approach right now is it:

  1. build an interface will the functionality I need.
  2. create a class that implements the interface, using my 3rd party component inside this class.
  3. any usage of this component will be via the interface so like:

    IPop3 pop3 = new AcmeIncePop3Wrapper();
    pop3.connect();

and inside AcmeIncePop3Wrapper will be:

   public void connect()
   {
         AcmeIncePop3 pop = new AcmeIncePop3();
         pop.connect();
   }

Is that a good approach?

I could probably add another abstraction by using ninject so I could swap out implementations, but really this seems to be all I need as i don't expect to be changing 3rd party assemblies every day, just don't want to make things so tightly coupled.

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

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

发布评论

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

评论(3

哭泣的笑容 2024-08-18 04:12:04

我认为你有一个很好的实施。基本上你的界面说“这就是我想要处理电子邮件的方式”,第三方组件可能会做不同的事情,但是如果你基于你的界面构建包装类,你应该能够将任何第三方组件工作到该界面中。

您可能需要注意的唯一一件事是在哪里更新具体类。如果你经常更新它,你可以考虑使用工厂或其他东西 - 如果不是,那可能没什么大不了的。我只是在想,如果您在各处创建 new AcmeIncePop3() ,那么它仍然与该具体类紧密耦合。

I think you have a good implementation. Basically your interface says "This is how I want to deal with email" and the third party components may do things differently, but if you build your wrapper classes based on your interface you should be able to work any third party component into that interface.

The only thing you may want to watch out for is where you are newing up the concrete classes. You could consider using a factory or something if you're newing it up alot - if not it may not be a big deal. I'm just thinking that if you create new AcmeIncePop3() all over the place then it's still rather tightly coupled to that concrete class.

如果没结果 2024-08-18 04:12:04

我想说你的实现很好,这正是我包装某些东西时要做的事情。

I'd say your implementation is fine, that's exactly the sort of thing I'd do to wrap something.

春风十里 2024-08-18 04:12:04

你的做法是对的。

简单地使用接口并不能解决这个问题,因为如果它位于第 3 方程序集中,则无法将接口添加到所需类的定义中。相反,执行以下两件事之一:

  1. 创建一个全新的类
    不同方法通用的方法
    您知道并处理的组件
    每个不同的组件可以
    存在(额外的逻辑可能是
    这里需要处理主要的
    差异)。

  2. 创建一个通用的接口
    方法等,然后创建一个新的
    继承自第3个类
    党课并落实
    界面。为每个班级执行此操作
    您需要使用的新程序集。

选项 2 的问题是您无法扩展密封类,而我希望第三方能够这样做。
我推荐选项1。

You're on the right lines.

Simply using an interface wouldn't solve it, as you can't add the interface to the definition to the class you need if it is located in a 3rd party assembly. Instead, do one of two things:

  1. create a whole new class with
    methods common to all the different
    assemblies you know of, and handle
    each different assembly that can
    exist (additional logic might be
    needed here to handle major
    differences).

  2. create an interface with common
    methods etc., then create a new
    class that inherits from the 3rd
    party class and implements the
    interface. Do this for each class in
    new assemblies that you need to use.

The problem with option 2 is that you can't extend a sealed class, which I would expect a third party to do.
I recommend option 1.

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