问题描述
我创建了一个提供消息、电子邮件和短信的类库(程序集).该类库定义了一个接口 IMessenger,EmailMessage 和 SmsMessage 类都实现了该接口.
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?
或者我应该做其他事情吗?
Or should I be doing something else?
推荐答案
这完全取决于您对 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.
如果您的实现类引用了与您的组织相关的其他代码或您不希望公开的任何内容,那么将 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.
这篇关于组件松耦合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!