在 Autofac 的上下文中:服务和组件有什么区别?
In the context of Autofac: What is the difference between a Service and a Component?
Service
和 Component
到底有什么区别?扩展方法 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();
}
}
或者组件总是 service/interface 的实现?我只是不明白。
如果有人能用一个容易理解的例子来澄清,我将不胜感激。
A Component
是经过解析处理后的具体代码。
一个Component
被一个或多个服务描述。即:Component
是 ServiceA
一个Service
用来描述一个Component
,将用来定义组件之间的关系。即:ComponentA
需要 serviceB
和 serviceC
。
在下面的代码中
builder.RegisterType<XXX>()
.As<IA>()
.Named<IB>("X");
XXX
将是类型化服务和命名服务所描述的 Component
。
Service
和 Component
到底有什么区别?扩展方法 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();
}
}
或者组件总是 service/interface 的实现?我只是不明白。
如果有人能用一个容易理解的例子来澄清,我将不胜感激。
A Component
是经过解析处理后的具体代码。
一个Component
被一个或多个服务描述。即:Component
是 ServiceA
一个Service
用来描述一个Component
,将用来定义组件之间的关系。即:ComponentA
需要 serviceB
和 serviceC
。
在下面的代码中
builder.RegisterType<XXX>()
.As<IA>()
.Named<IB>("X");
XXX
将是类型化服务和命名服务所描述的 Component
。