Skip to content

Latest commit

 

History

History
155 lines (131 loc) · 4.3 KB

File metadata and controls

155 lines (131 loc) · 4.3 KB

OnDependencyInjection regular expression hint

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))
    // Filters types by regular expression to control which types trigger the OnDependencyInjection method.
    // In this case, we want to intercept the injection of any "Gateway" (like IPaymentGateway)
    // and integer configuration values.
    .Hint(OnDependencyInjectionContractTypeNameRegularExpression, "(.*Gateway|int)$")
    .RootArg<int>("maxAttempts")
    .Bind().To<PayPalGateway>()
    .Bind().To<PaymentService>()
    .Root<IPaymentService>("GetPaymentService");

var log = new List<string>();
var composition = new Composition(log);

// Resolving the root service triggers the injection chain.
// 1. int maxAttempts is injected into PayPalGateway.
// 2. PayPalGateway is injected into PaymentService.
// PaymentService itself is not logged because "IPaymentService" does not match the regex.
var service = composition.GetPaymentService(3);

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

interface IPaymentGateway;

record PayPalGateway(int MaxAttempts) : IPaymentGateway;

interface IPaymentService
{
    IPaymentGateway Gateway { get; }
}

class PaymentService(IPaymentGateway gateway) : IPaymentService
{
    public IPaymentGateway Gateway { get; } = gateway;
}

partial class Composition(List<string> log)
{
    private partial T OnDependencyInjection<T>(
        in T value,
        object? tag,
        Lifetime lifetime)
    {
        // Logs the actual runtime type of the injected instance
        log.Add($"{value?.GetType().Name} injected");
        return value;
    }
}
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

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
{
  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public IPaymentService GetPaymentService(int maxAttempts)
  {
    return new PaymentService(OnDependencyInjection<IPaymentGateway>(new PayPalGateway(OnDependencyInjection<int>(maxAttempts, 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
	PayPalGateway --|> IPaymentGateway
	PayPalGateway --|> IEquatableᐸPayPalGatewayᐳ
	PaymentService --|> IPaymentService
	Composition ..> PaymentService : IPaymentService GetPaymentService(int maxAttempts)
	PayPalGateway o-- Int32 : Argument "maxAttempts"
	PaymentService *-- PayPalGateway : IPaymentGateway
	namespace Pure.DI.UsageTests.Hints.OnDependencyInjectionRegularExpressionHintScenario {
		class Composition {
		<<partial>>
		+IPaymentService GetPaymentService(int maxAttempts)
		}
		class IPaymentGateway {
			<<interface>>
		}
		class IPaymentService {
			<<interface>>
		}
		class PaymentService {
				<<class>>
			+PaymentService(IPaymentGateway gateway)
		}
		class PayPalGateway {
				<<record>>
			+PayPalGateway(Int32 MaxAttempts)
		}
	}
	namespace System {
		class IEquatableᐸPayPalGatewayᐳ {
			<<interface>>
		}
		class Int32 {
				<<struct>>
		}
	}
Loading