Skip to content

Side Tuners Fine Grained Control Over Dependency Injection

Ed Pavlov edited this page Jun 8, 2024 · 1 revision

Side Tuners: Fine-Grained Control Over Dependency Injection

Side Tuners in Armature offer a flexible mechanism for applying targeted modifications to the dependency injection process. Unlike primary tuners that focus on specific unit types or contexts, side tuners act as modifiers or decorators, allowing you to refine the behavior of existing build configurations.

The ISideTuner Interface

The ISideTuner interface provides the foundation for defining side tuners.

It has a single method:

ApplyTo(ITunerBase tuner): This method is called to apply the side tuner's modifications to the specified tuner. The modifications typically involve adding or modifying build stack patterns and build actions to achieve the desired behavior.

Inheritance from ISideTuner: Enhancing Syntax

The inheritance of specialized side tuners, such as IInjectionPointSideTuner and IArgumentSideTuner, from ISideTuner is a deliberate design choice aimed at improving the syntax and readability of Armature's fluent API. While primary tuners (like Treat<T>) are typically chained together in a fluent manner, side tuners are passed as arguments to specific methods of the primary tuners.

This approach allows for a more natural and expressive syntax.

builder
  .Treat<MyClass>()
  .UsingInjectionPoints(Constructor.WithParameters(typeof(ILogger), typeof(IConfiguration)));


builder
  .Treat<MyClass>()
  .UsingArguments(ForParameter.Named("parameterName").UseValue("value"));

Constructor.WithParameters and ForParameter.Named returns instances of types inheriting ISideTuner interface.

IArgumentSideTuner: Fine-Tuning Argument Resolution

The primary purpose of IArgumentSideTuner is to provide fine-grained control over how arguments are resolved and passed to injection points. It allows you to specify:

  • Argument Values: Directly provide specific values to be injected as arguments.
  • Factory Methods: Use custom factory methods to create argument values on the fly.
  • Argument Tags: Associate tags with arguments to enable more precise matching based on context.

By applying an IArgumentSideTuner, you can override your Builder's default argument resolution logic and tailor it to your specific needs.

IInjectionPointSideTuner: Fine-Tuning Injection Point Selection

The IInjectionPointSideTuner interface specializes in refining the selection of injection points (constructor/methods parameters or properties) for dependency injection. It allows you to specify criteria such as:

  • Member Type: Target specific types of members (e.g., constructors, properties).
  • Attributes: Select members based on the presence of specific attributes (e.g., InjectAttribute).
  • Parameter Names and Types: Choose constructors based on the types or names of their parameters.
  • Property Names and Types: Select properties by their types or names.

By applying an IInjectionPointSideTuner, you can override your Builder's default injection point selection logic and tailor it to your specific requirements.