Skip to content

Latest commit

 

History

History
132 lines (110 loc) · 3.14 KB

File metadata and controls

132 lines (110 loc) · 3.14 KB

Tag on a method argument

The wildcards * and ? are supported.

using Shouldly;
using Pure.DI;

DI.Setup(nameof(Composition))
    .Bind().To<TemperatureSensor>()
    // Binds specifically to the argument "sensor" of the "Calibrate" method
    // in the "WeatherStation" class
    .Bind(Tag.OnMethodArg<WeatherStation>(nameof(WeatherStation.Calibrate), "sensor"))
    .To<HumiditySensor>()
    .Bind<IWeatherStation>().To<WeatherStation>()

    // Specifies to create the composition root named "Station"
    .Root<IWeatherStation>("Station");

var composition = new Composition();
var station = composition.Station;
station.Sensor.ShouldBeOfType<HumiditySensor>();

interface ISensor;

class TemperatureSensor : ISensor;

class HumiditySensor : ISensor;

interface IWeatherStation
{
    ISensor? Sensor { get; }
}

class WeatherStation : IWeatherStation
{
    // The [Dependency] attribute is used to mark the method for injection
    [Dependency]
    public void Calibrate(ISensor sensor) =>
        Sensor = sensor;

    public ISensor? Sensor { get; private set; }
}
Running this code sample locally
dotnet --list-sdk
  • Create a net10.0 (or later) console application
dotnet new console -n Sample
dotnet add package Pure.DI
dotnet add package Shouldly
  • Copy the example code into the Program.cs file

You are ready to run the example 🚀

dotnet run

Warning

Each potentially injectable argument, property, or field contains an additional tag. This tag can be used to specify what can be injected there. This will only work if the binding type and the tag match. So while this approach can be useful for specifying what to enter, it can be more expensive to maintain and less reliable, so it is recommended to use attributes like [Tag(...)] instead.

The following partial class will be generated:

partial class Composition
{
  public IWeatherStation Station
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      var transientWeatherStation130 = new WeatherStation();
      transientWeatherStation130.Calibrate(new HumiditySensor());
      return transientWeatherStation130;
    }
  }
}

Class diagram:

---
 config:
  maxTextSize: 2147483647
  maxEdges: 2147483647
  class:
   hideEmptyMembersBox: true
---
classDiagram
	HumiditySensor --|> ISensor
	WeatherStation --|> IWeatherStation
	Composition ..> WeatherStation : IWeatherStation Station
	WeatherStation *--  HumiditySensor : ISensor
	namespace Pure.DI.UsageTests.Advanced.TagOnMethodArgScenario {
		class Composition {
		<<partial>>
		+IWeatherStation Station
		}
		class HumiditySensor {
				<<class>>
			+HumiditySensor()
		}
		class ISensor {
			<<interface>>
		}
		class IWeatherStation {
			<<interface>>
		}
		class WeatherStation {
				<<class>>
			+WeatherStation()
			+Calibrate(ISensor sensor) : Void
		}
	}
Loading