c# - 在 Autofac : What is the difference between a Service and a Component? 的上下文中

标签 c# inversion-of-control autofac

服务组件之间到底有什么区别?扩展方法 RegisterComponent() 与此定义有何关系?

Autofac 的术语表对其定义如下:

组件

A body of code that declares the Services it provides and the Dependencies it consumes

服务

A well-defined behavioural contract shared between a providing and a consuming Component

这让我很困惑。一个组件使用多个服务这样说是否正确?像下面的例子吗?

public interface IServiceA
{
    void DoSomething();
}

public ServiceA : IServiceA
{
    void DoSomething()
    {
        // Do some magic 
    }
}

public class ComponentA
{
    private readonly IServiceA serviceA;

    public ComponentA(IServiceA serviceA)
    {
        this.serviceA = serviceA;
    }

    public void SomeOperation()
    {
        this.serviceA.DoSomething();
    }
}

或者组件总是服务/接口(interface)的实现?我就是不明白。

如果有人能用一个容易理解的例子来澄清,我将不胜感激。

最佳答案

组件是在解析过程之后将成为具体代码的东西。

组件由一个或多个服务描述。即: ComponentServiceA

Service用于描述Component,并用于定义组件之间的关系。即:ComponentA 需要 serviceBserviceC

在下面的代码中

builder.RegisterType<XXX>()
       .As<IA>()
       .Named<IB>("X"); 

XXX 将是由类型化服务和命名服务描述的Component

关于c# - 在 Autofac : What is the difference between a Service and a Component? 的上下文中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60915780/

相关文章:

c# - 同一文件的大小差异,在 Windows XP 和 Windows 7 上阅读

c# - 如果值为空格,则不会调用 FileHelpers FieldConverter

c# - .NET 在远程 SQL Server 数据库中缓慢插入

c# - 具有 Autofac 依赖项的工厂

c# - 使用 DI/autofac 避免嵌套服务定位器反模式

c# - 使用可选参数对象注册 autofac 组件

C# MVC 与 ASP.NET MVC 的区别?

ios - 台风+swift自动注入(inject)

c# - IoC 链中断

java - 如何实现用组单例分隔的 spring