Skip to content

Latest commit

 

History

History
159 lines (137 loc) · 3.87 KB

File metadata and controls

159 lines (137 loc) · 3.87 KB

Array

Specifying T[] as the injection type allows instances from all bindings that implement the T type to be injected.

using Shouldly;
using Pure.DI;

DI.Setup(nameof(Composition))
    .Bind<ISensor>().To<TemperatureSensor>()
    .Bind<ISensor>("External").To<WindSensor>()
    .Bind<ISensorService>().To<SensorService>()

    // Composition root
    .Root<ISensorService>("Sensor");

var composition = new Composition();
var sensor = composition.Sensor;

// Checks that all bindings for the ISensor interface are injected,
// regardless of whether they are tagged or not.
sensor.Sensors.Length.ShouldBe(2);
sensor.Sensors[0].ShouldBeOfType<TemperatureSensor>();
sensor.Sensors[1].ShouldBeOfType<WindSensor>();

interface ISensor;

class TemperatureSensor : ISensor;

class WindSensor : ISensor;

interface ISensorService
{
    ISensor[] Sensors { get; }
}

class SensorService(ISensor[] sensors) : ISensorService
{
    public ISensor[] Sensors { get; } = sensors;
}
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

In addition to arrays, other collection types are also supported, such as:

  • System.Memory
  • System.ReadOnlyMemory
  • System.Span
  • System.ReadOnlySpan
  • System.Collections.Generic.ICollection
  • System.Collections.Generic.IList
  • System.Collections.Generic.List
  • System.Collections.Generic.IReadOnlyCollection
  • System.Collections.Generic.IReadOnlyList
  • System.Collections.Generic.ISet
  • System.Collections.Generic.HashSet
  • System.Collections.Generic.SortedSet
  • System.Collections.Generic.Queue
  • System.Collections.Generic.Stack
  • System.Collections.Immutable.ImmutableArray
  • System.Collections.Immutable.IImmutableList
  • System.Collections.Immutable.ImmutableList
  • System.Collections.Immutable.IImmutableSet
  • System.Collections.Immutable.ImmutableHashSet
  • System.Collections.Immutable.ImmutableSortedSet
  • System.Collections.Immutable.IImmutableQueue
  • System.Collections.Immutable.ImmutableQueue
  • System.Collections.Immutable.IImmutableStack And of course this list can easily be supplemented on its own.

The following partial class will be generated:

partial class Composition
{
  public ISensorService Sensor
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      return new SensorService(new ISensor[2] { new TemperatureSensor(), new WindSensor() });
    }
  }
}

Class diagram:

---
 config:
  maxTextSize: 2147483647
  maxEdges: 2147483647
  class:
   hideEmptyMembersBox: true
---
classDiagram
	TemperatureSensor --|> ISensor
	WindSensor --|> ISensor : "External" 
	SensorService --|> ISensorService
	Composition ..> SensorService : ISensorService Sensor
	SensorService *--  ArrayᐸISensorᐳ : ArrayᐸISensorᐳ
	ArrayᐸISensorᐳ *--  TemperatureSensor : ISensor
	ArrayᐸISensorᐳ *--  WindSensor : "External"  ISensor
	class ArrayᐸISensorᐳ {
			<<array>>
	}
	namespace Pure.DI.UsageTests.BCL.ArrayScenario {
		class Composition {
		<<partial>>
		+ISensorService Sensor
		}
		class ISensor {
			<<interface>>
		}
		class ISensorService {
			<<interface>>
		}
		class SensorService {
				<<class>>
			+SensorService(ArrayᐸISensorᐳ sensors)
		}
		class TemperatureSensor {
				<<class>>
			+TemperatureSensor()
		}
		class WindSensor {
				<<class>>
			+WindSensor()
		}
	}
Loading