Skip to content

Latest commit

 

History

History
155 lines (130 loc) · 3.43 KB

File metadata and controls

155 lines (130 loc) · 3.43 KB

Bind attribute with lifetime and tag

Demonstrates how to configure the Bind attribute with lifetime and tag parameters for more precise binding control.

using Shouldly;
using Pure.DI;

DI.Setup(nameof(Composition))
    .Bind().As(Lifetime.Singleton).To<GraphicsAdapter>()
    .Bind().To<RayTracer>()

    // Composition root
    .Root<IRenderer>("Renderer");

var composition = new Composition();
var renderer = composition.Renderer;
renderer.Render();

interface IGpu
{
    void RenderFrame();
}

class DiscreteGpu : IGpu
{
    public void RenderFrame() => Console.WriteLine("Rendering with Discrete GPU");
}

class GraphicsAdapter
{
    // Binds the property to the composition with the specified
    // lifetime and tag. This allows the "HighPerformance" GPU
    // to be injected into other components.
    [Bind(lifetime: Lifetime.Singleton, tags: ["HighPerformance"])]
    public IGpu HighPerfGpu { get; } = new DiscreteGpu();
}

interface IRenderer
{
    void Render();
}

class RayTracer([Tag("HighPerformance")] IGpu gpu) : IRenderer
{
    public void Render() => gpu.RenderFrame();
}
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

Note

Specifying lifetime and tag in the Bind attribute allows for fine-grained control over instance creation and binding resolution.

The following partial class will be generated:

partial class Composition
{
#if NET9_0_OR_GREATER
  private readonly Lock _lock = new Lock();
#else
  private readonly Object _lock = new Object();
#endif

  private IGpu? _singletonIGpu2147483143;
  private GraphicsAdapter? _singletonGraphicsAdapter62;

  public IRenderer Renderer
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      if (_singletonIGpu2147483143 is null)
        lock (_lock)
          if (_singletonIGpu2147483143 is null)
          {
            if (_singletonGraphicsAdapter62 is null)
            {
              _singletonGraphicsAdapter62 = new GraphicsAdapter();
            }

            GraphicsAdapter localInstance_1182D1279 = _singletonGraphicsAdapter62;
            _singletonIGpu2147483143 = localInstance_1182D1279.HighPerfGpu;
          }

      return new RayTracer(_singletonIGpu2147483143);
    }
  }
}

Class diagram:

---
 config:
  maxTextSize: 2147483647
  maxEdges: 2147483647
  class:
   hideEmptyMembersBox: true
---
classDiagram
	RayTracer --|> IRenderer
	Composition ..> RayTracer : IRenderer Renderer
	RayTracer o-- "Singleton" IGpu : "HighPerformance"  IGpu
	IGpu o-- "Singleton" GraphicsAdapter : GraphicsAdapter
	namespace Pure.DI.UsageTests.Basics.BindAttributeWithLifetimeAndTagScenario {
		class Composition {
		<<partial>>
		+IRenderer Renderer
		}
		class GraphicsAdapter {
				<<class>>
			+GraphicsAdapter()
		}
		class IGpu {
				<<interface>>
		}
		class IRenderer {
			<<interface>>
		}
		class RayTracer {
				<<class>>
			+RayTracer(IGpu gpu)
		}
	}
Loading