Service registration

The registration is done with the Litium.Runtime.DependencyInjection.Service-attribute with which you decorate the service contract or service implementation.

During startup all classes that is decorated with the attribute is fetched and registered with the dependency injection engine. If multiple instances of a single contract (interface or abstract class) is found and only one item is expected the registration process will find the first implementation based on assembly naming. To alter the order the assembly can be decorated with the following attribute.

[assembly: Litium.Runtime.AssemblyLoading(Litium.Runtime.AssemblyLoadOrder.First)]

Example of service registration

[Litium.Runtime.DependencyInjection.Service(
    ServiceType = typeof(IMyService), 
    Lifetime = Litium.Runtime.DependencyInjection.DependencyLifetime.Transient)]
public interface IMyService
{
    void MyMethod(); // define the service contract members
}

internal class MyServiceImpl : IMyService
{
    public void MyMethod()
    {
        // do something in the method
    }
}

Example of usage the service registration

public class DependencyInjectionExample
{
    private readonly IMyService _myService;

    public DependencyInjectionExample(IMyService myService)
    {
        _myService = myService;
    }

    public void Method()
    {
        _myService.MyMethod();
    }
}