2

What exactly is the difference between a Service and a Component? And how does the extension method RegisterComponent() relate to this definitions?

Autofac's glossary defines it as follows:

Component

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

Service

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

This confuses me.. Would it be correct to say, that a component uses several services? Something like the example below?

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();
    }
}

Or is a component always an implementation of a service/interface? I just don't get it.

I would be thankful if somebody could clarify with a catchable example.

Matthias Güntert
  • 4,013
  • 6
  • 41
  • 89
  • 1
    This is a bit a simplification, but you can replace the word "Component" with "class" or "implementation", and replace the word "Service" with "interface" or "abstraction." – Steven Mar 30 '20 at 11:49

1 Answers1

2

A Component is something that will be concrete piece of code after the resolution process.

A Component is described by one or multiple service. ie : Component is ServiceA

A Service is used to describe a Component and will be used to define relation between component. ie : ComponentA needs serviceB and serviceC.

In the following code

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

XXX would be the Component described by a typed service and a named service.

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62
  • Thx for replying. So in my example from above a `component` is an implementation of `IServiceA` and the interface itself is the (describing) service? – Matthias Güntert Mar 29 '20 at 15:05
  • In your case you can think of Component and Service like class and interface – Cyril Durand Mar 29 '20 at 15:07
  • For reference, [this is documented](https://autofac.readthedocs.io/en/latest/register/registration.html) and there is [a glossary](https://autofac.readthedocs.io/en/latest/glossary.html). – Travis Illig Mar 30 '20 at 04:14