Skip to content

Latest commit

 

History

History
131 lines (109 loc) · 3.16 KB

File metadata and controls

131 lines (109 loc) · 3.16 KB

Tag on a member

The wildcards * and ? are supported.

using Shouldly;
using Pure.DI;

DI.Setup(nameof(Composition))
    .Bind().To<PayPalGateway>()
    // Binds StripeGateway to the "Gateway" property of the "CheckoutService" class.
    // This lets you override the injected dependency for a specific member
    // without changing the class definition.
    .Bind(Tag.OnMember<CheckoutService>(nameof(CheckoutService.Gateway)))
    .To<StripeGateway>()
    .Bind<ICheckoutService>().To<CheckoutService>()

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

var composition = new Composition();
var checkoutService = composition.CheckoutService;

// Checks that the property was injected with the specific implementation
checkoutService.Gateway.ShouldBeOfType<StripeGateway>();

interface IPaymentGateway;

class PayPalGateway : IPaymentGateway;

class StripeGateway : IPaymentGateway;

interface ICheckoutService
{
    IPaymentGateway Gateway { get; }
}

class CheckoutService : ICheckoutService
{
    public required IPaymentGateway Gateway { init; get; }
}
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 ICheckoutService CheckoutService
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      return new CheckoutService()
      {
        Gateway = new StripeGateway()
      };
    }
  }
}

Class diagram:

---
 config:
  maxTextSize: 2147483647
  maxEdges: 2147483647
  class:
   hideEmptyMembersBox: true
---
classDiagram
	StripeGateway --|> IPaymentGateway
	CheckoutService --|> ICheckoutService
	Composition ..> CheckoutService : ICheckoutService CheckoutService
	CheckoutService *--  StripeGateway : IPaymentGateway
	namespace Pure.DI.UsageTests.Advanced.TagOnMemberScenario {
		class CheckoutService {
				<<class>>
			+CheckoutService()
			+IPaymentGateway Gateway
		}
		class Composition {
		<<partial>>
		+ICheckoutService CheckoutService
		}
		class ICheckoutService {
			<<interface>>
		}
		class IPaymentGateway {
			<<interface>>
		}
		class StripeGateway {
				<<class>>
			+StripeGateway()
		}
	}
Loading