服务
和组件
之间到底有什么区别?扩展方法 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)的实现?我就是不明白。
如果有人能用一个容易理解的例子来澄清,我将不胜感激。
最佳答案
组件
是在解析过程之后将成为具体代码的东西。
组件
由一个或多个服务描述。即: Component
是 ServiceA
Service
用于描述Component
,并用于定义组件之间的关系。即:ComponentA
需要 serviceB
和 serviceC
。
在下面的代码中
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/