Skip to content

Latest commit

 

History

History
165 lines (136 loc) · 3.95 KB

ondependencyinjection-regular-expression-hint.md

File metadata and controls

165 lines (136 loc) · 3.95 KB

OnDependencyInjection regular expression hint

CSharp

Hints are used to fine-tune code generation. The OnDependencyInjection hint determines whether to generate partial OnDependencyInjection method to control of dependency injection. In addition, setup hints can be comments before the Setup method in the form hint = value, for example: // OnDependencyInjection = On.

using Shouldly;
using Pure.DI;
using static Pure.DI.Hint;

// OnDependencyInjection = On
DI.Setup(nameof(Composition))
    .Hint(OnDependencyInjectionContractTypeNameRegularExpression, "(.*IDependency|int)$")
    .RootArg<int>("id")
    .Bind().To<Dependency>()
    .Bind().To<Service>()
    .Root<IService>("GetRoot");

var log = new List<string>();
var composition = new Composition(log);
var service = composition.GetRoot(33);

log.ShouldBe([
    "Int32 injected",
    "Dependency injected"
]);

interface IDependency;

record Dependency(int Id) : IDependency;

interface IService
{
    IDependency Dependency { get; }
}

class Service(IDependency dependency) : IService
{
    public IDependency Dependency { get; } = dependency;
}

partial class Composition
{
    private readonly List<string> _log = [];

    public Composition(List<string> log) : this() =>
        _log = log;

    private partial T OnDependencyInjection<T>(
        in T value,
        object? tag,
        Lifetime lifetime)
    {
        _log.Add($"{value?.GetType().Name} injected");
        return value;
    }
}
Running this code sample locally
dotnet --list-sdk
  • Create a net9.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

The OnDependencyInjectionContractTypeNameRegularExpression hint helps identify the set of types that require injection control. You can use it to specify a regular expression to filter the full name of a type. For more hints, see this page.

The following partial class will be generated:

partial class Composition
{
  private readonly Composition _root;

  [OrdinalAttribute(128)]
  public Composition()
  {
    _root = this;
  }

  internal Composition(Composition parentScope)
  {
    _root = (parentScope ?? throw new ArgumentNullException(nameof(parentScope)))._root;
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public IService GetRoot(int id)
  {
    return new Service(OnDependencyInjection<IDependency>(new Dependency(OnDependencyInjection<int>(id, null, Lifetime.Transient)), null, Lifetime.Transient));
  }


  private partial T OnDependencyInjection<T>(in T value, object? tag, Lifetime lifetime);
}

Class diagram:

---
 config:
  class:
   hideEmptyMembersBox: true
---
classDiagram
	Service --|> IService
	Dependency --|> IDependency
	Dependency --|> IEquatableᐸDependencyᐳ
	Composition ..> Service : IService GetRoot(int id)
	Service *--  Dependency : IDependency
	Dependency o-- Int32 : Argument "id"
	namespace Pure.DI.UsageTests.Hints.OnDependencyInjectionRegularExpressionHintScenario {
		class Composition {
		<<partial>>
		+IService GetRoot(int id)
		}
		class Dependency {
				<<record>>
			+Dependency(Int32 Id)
		}
		class IDependency {
			<<interface>>
		}
		class IService {
			<<interface>>
		}
		class Service {
			+Service(IDependency dependency)
		}
	}
	namespace System {
		class IEquatableᐸDependencyᐳ {
			<<interface>>
		}
		class Int32 {
				<<struct>>
		}
	}
Loading