组件的松耦合组件

2023-09-03 23:08:44 作者:Spume‘浅爱ゝ

我创建了一个类库(组装),提供信息,电子邮件和短信。这个类库定义了类EmailMessage和SmsMessage都实现一个接口IMessenger的。

I have created a class library (assembly) that provides messaging, email and sms. This class library defines an interface IMessenger which the classes EmailMessage and SmsMessage both implement.

我认为这是这将是我的基础架构层的一部分,并会/可以在任何开发中使用的通用库。

I see this is a general library that would be part of my infrastructure layer and would / can be used across any development.

现在,在我的应用程序层,我有需要使用一个消息组件类,我明明想用我所创建的消息库。此外,我将使用IoC容器(Spring.net),让我注入我的实现,即电子邮件或短信。

Now, in my application layer I have a class that requires to use a messaging component, I obviously want to use the messaging library that I have created. Additionally, I will be using an IoC container (Spring.net) to allow me to inject my implementation i.e. either email or sms.

所以,我想对我的应用程序层类的接口程序,我则需要引用我的消息类库,从我的应用程序层类?

Therefore, I want to program against an interface in my application layer class, do I then need to reference my message class library from my application layer class?

时此紧密耦合我的应用程序层类给我留言类库?

Is this tightly coupling my application layer class to my message class library?

我应该定义界面 - ?IMessenger的在一个单独的库

Should I be defining the interface - IMessenger in a seperate library?

或者我应该做点别的?

推荐答案

这一切都取决于你的未来IMessenger的计划。我个人没有找到具有界面以及一些示例实现在同一个程序集是一件坏事。如果有人想提供一个新的实现IMessenger的,他们将不得不做,在另一个程序集,以及使你的程序集的过程中,他们会得到EmailMessage,SmsMessage等。对于一个轻量级组件,我认为这是不是一个大问题,因为它可以节省您要使用一个每次都引用这两个你的接口和实施组件的努力。

It all depends on what your future plans for IMessenger are. Personally I do not find that having the interface as well as a few sample implementations in the same assembly is a bad thing. If someone wants to provide a new implementation of IMessenger they will have to do it in another assembly, and in the course of bringing in your assembly they will get EmailMessage, SmsMessage, etc. For a lightweight assembly I view that as not a big deal, since it saves you the effort of referencing both your "interface" and "implementation" assemblies every time you want to use one.

如果您的实现类引用其他code相关的组织或任何你不希望暴露出来,那就更有意义IMessenger的分离到它自己的组件,并根据需要只分发。

If your implementation classes reference other code related to your organization or anything that you wouldn't want exposed, then it would make more sense to separate IMessenger into its own assembly and just distribute that as needed.