C# 中松耦合的最佳工具是什么

发布于 2024-08-17 03:22:19 字数 122 浏览 3 评论 0原文

我正在阅读《松耦合及其好处》,这确实是好东西,但我开始想知道哪些工具非常适合创建松耦合解决方案?首先我想到的是类型、接口和抽象类,但我确信有很多方法可以提供松散耦合。也许多态有助于创建松散耦合的对象和系统。

谢谢。

I was reading Loose Coupling and its benefits which are really good stuff but I started wondering which tools are great to create loosely-coupled solutions ? First what came to my mind are Type and Interfaces and Abstract classes but I am sure there are so many ways to provide loose coupling. Maybe Polymorphism helps to create loosely-coupled objects and systems.

Thanks.

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

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

发布评论

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

评论(7

夏有森光若流苏 2024-08-24 03:22:19

这是一个非常广泛的问题 - 可能太广泛而无法回答。

然而,可以提供“松散耦合”解决方案的两种方法是:

依赖注入

依赖注入(特别是在使用控制反转容器时)在应用程序或解决方案中提供松散耦合 - 通过依赖注入,您可以不要对依赖对象的引用进行硬编码,而是对接口进行编码并注入这些接口的实现。

下面是应用程序的日志记录组件的快速代码示例,它实现了 ILogger 接口,

public class ConcreteLogger : ILogger
{

    public LogMessage(string message)
    {
        Log.Write(message);
    }
}

然后您的一个类接收记录器接口的具体实现。

public class MyClass
{
    private ILogger logger;

    public myClass(ILogger logger)
    {
        this.logger = logger;
    }

    public void DoSomething()
    {
        // Now if DoSomething needs to call logging it can call what ever ILogger was passed in
        this.logger.Log("We did something");
    }
}

面向服务

面向服务在解决方案的不同子系统之间提供松散耦合 - 通过面向服务,解决方案的每个部分都成为其自己的发布和接口(通常是基于消息传递的接口)的独立服务。

这意味着,当应用程序需要与解决方案的运输子系统通信时,只需了解系统接口及其地址,而无需了解其内部实现的任何信息。这几乎可以被视为基本 OO 原则的更广泛应用。


这两者都提供了两种不同类型的松散耦合(这是您的问题的问题之一,术语它本身是松散定义的)

This is a very broad question - possibly too broad to answer.

However, two methodologies that can provide a solution with "loose coupling" are:

Dependency Injection

Dependency Injection (especially when using an Inversion of Control Container) provides loose coupling within an application or solution - With dependency injection you do not hard code your references to dependent objects, instead you code to interfaces and inject implementations of those interfaces.

Here is a quick code example of a Logging component of an application, that implements an ILogger interface

public class ConcreteLogger : ILogger
{

    public LogMessage(string message)
    {
        Log.Write(message);
    }
}

One of your classes then receives this concrete implementation of the logger interface.

public class MyClass
{
    private ILogger logger;

    public myClass(ILogger logger)
    {
        this.logger = logger;
    }

    public void DoSomething()
    {
        // Now if DoSomething needs to call logging it can call what ever ILogger was passed in
        this.logger.Log("We did something");
    }
}

Service Orientation

Service Orientation provides loose coupling between different subsystems of a solution - With Service Orientation each part of a solution becomes its own stand alone services that publishes and interfaces (often a messaging based interface).

This means that an application when needing for example to talk to the Shipping subsystem of a solution only needs to know about the systems interfaces and its address, and does not need to know anything about its internal implementation. This can almost be seen as a broader application of basic OO principles.


Both of these provide two different types of loose coupling (that is one of the problems with your question, the term it self is loosely defined)

黄昏下泛黄的笔记 2024-08-24 03:22:19

其他人已经提供了很好的答案,但我想补充一个想法:使用继承实现的多态性不会促进松散耦合。继承是基类和派生类之间非常强的耦合。

Others have already provided great answers but I want to add one thought: polymorphism as implemented using inheritance does not promote loose-coupling. Inheritance is a very strong coupling between base classes and derived classes.

醉生梦死 2024-08-24 03:22:19

你的大脑大概?

编程时要考虑接口。将您的库构建为具有最少且显式接口的独立代码小岛。限制所有符号的范围,特别是可变符号(例如变量)。不要泄露实施细节,除非它们是相关的。

Your brain presumably?

Program with interfaces in mind. Build your libraries as small islands of self-contained code with minimal and explicit interfaces. Limit scope of all symbols, but in particular mutable ones (eg. variables). Don't give away implementation details, unless they are relevant.

清晰传感 2024-08-24 03:22:19
  • 良好的 OOD
  • 使用
  • IoC 接口(DI,例如 Microsoft.Practices.Unity 或 Windsor 等)
  • 适当的设计模式(MVC、n 层等)
  • Good OOD
  • Use of Interfaces
  • IoC (DI such as Microsoft.Practices.Unity or Windsor etc)
  • Appropriate design patterns (MVC, n-tier etc)
私野 2024-08-24 03:22:19

MEF是松耦合的最佳工具!从一开始就将您的应用程序设计得对 MEF/插件非常友好,您会发现您的应用程序将非常松散耦合

MEF is the best tool for loose coupling! Design your apps from the start to be very MEF/plugin friendly, and you'll find that your app will be very loosely coupled

神也荒唐 2024-08-24 03:22:19

放松耦合的一个好方法是依赖注入

A great way to loosen coupling is Dependency Injection.

帥小哥 2024-08-24 03:22:19

迄今为止最好的工具是 Microsoft CAB (其中包括前面提到的Unity框架)
它包括用于依赖项注入、松散耦合事件处理等的工具。

The best tool by far is Microsoft CAB (which includes the previously mentioned Unity framework)
It includes tools for dependency injection, loosely-coupled event handling, and much more.

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