Skip to content

02. Basic usage

Tim Maes edited this page Oct 15, 2024 · 1 revision

After installing Bindicate, you can start using it to automatically register your services.

Registering services

Add the following line in your application's startup code to register all services decorated with Bindicate attributes:

builder.Services
    .AddAutowiringForAssembly(Assembly.GetExecutingAssembly())
    .Register();

Service registration with Attributes

Class-Only Registrations

Decorate your class with an attribute to register it with a specific lifetime.

[AddTransient]
public class SimpleTaskRunner
{
    public void RunTask()
    {
        // Implementation
    }
}

Interface Implementations

Decorate your class and specify the interface it implements

[AddScoped(typeof(IMyTaskRunner))]
public class TaskRunner : IMyTaskRunner
{
    public void Run()
    {
        // Implementation
    }
}

public interface IMyTaskRunner
{
    void Run();
}

Keyed Services

Keyed services enable resolving different implementations of the same interface based on a key.

Registering Keyed Services (NET8+)

Decorate your class with [AddKeyedScoped], [AddKeyedSingleton], or [AddKeyedTransient] and provide the key.

[AddKeyedScoped("serviceA", typeof(IMyService))]
public class ServiceA : IMyService
{
    // Implementation
}

[AddKeyedScoped("serviceB", typeof(IMyService))]
public class ServiceB : IMyService
{
    // Implementation
}

Resolving Keyed Services

Use the GetKeyedService extension method.

var serviceA = serviceProvider.GetKeyedService<IMyService>("serviceA");
var serviceB = serviceProvider.GetKeyedService<IMyService>("serviceB");

Generics

Registering Generic Interfaces

Decorate your generic interface with [RegisterGenericInterface].

[RegisterGenericInterface]
public interface IRepository<T>
{
    void Add(T entity);
}

Implementing Generic Services

Decorate your generic class and specify the open generic interface.

[AddTransient(typeof(IRepository<>))]
public class Repository<T> : IRepository<T>
{
    public void Add(T entity)
    {
        // Implementation
    }
}

Using Generic Services

Resolve the service for the specific type.

var customerRepository = serviceProvider.GetService<IRepository<Customer>>();
customerRepository.Add(new Customer());

TryAddEnumberable

Bindicate supports the TryAddEnumerable method, allowing multiple implementations of an interface to be registered and resolved as IEnumerable<T>.

Registering Multiple implementations

Decorate each implementation with [TryAddEnumerable], specifying the lifetime and service type.

[TryAddEnumerable(Lifetime.TryAddEnumerableTransient, typeof(IMyService))]
public class MyServiceA : IMyService
{
    // Implementation
}

[TryAddEnumerable(Lifetime.TryAddEnumerableTransient, typeof(IMyService))]
public class MyServiceB : IMyService
{
    // Implementation
}

Resolving Multiple Implementations

Inject IEnumberable<T> into your consuming class.

public class MyController
{
    private readonly IEnumerable<IMyService> _services;

    public MyController(IEnumerable<IMyService> services)
    {
        _services = services;
    }

    public void ExecuteAll()
    {
        foreach (var service in _services)
        {
            service.Execute();
        }
    }
}