Skip to content

Latest commit

 

History

History
158 lines (131 loc) · 3.72 KB

File metadata and controls

158 lines (131 loc) · 3.72 KB

Partial class

The composition class is generated as a partial class, so you can put the setup code in your own part of it and extend the generated code with hand-written members. Here the custom part adds a constructor accepting storeName and a GenerateId() method, and both are used directly inside factory bindings.

using Shouldly;
using Pure.DI;
using static Pure.DI.RootKinds;
using System.Diagnostics;

var composition = new Composition("NorthStore");
var orderService = composition.OrderService;

// Checks that the dependencies were created correctly
orderService.Order1.Id.ShouldBe(1);
orderService.Order2.Id.ShouldBe(2);
// Checks that the injected string contains the store name and the generated ID
orderService.OrderDetails.ShouldBe("NorthStore_3");

interface IOrder
{
    long Id { get; }
}

class Order(long id) : IOrder
{
    public long Id { get; } = id;
}

class OrderService(
    [Tag("Order details")] string details,
    IOrder order1,
    IOrder order2)
{
    public string OrderDetails { get; } = details;

    public IOrder Order1 { get; } = order1;

    public IOrder Order2 { get; } = order2;
}

// The partial class is also useful for specifying access modifiers to the generated class
public partial class Composition(string storeName)
{
    private long _id;

    private long GenerateId() => Interlocked.Increment(ref _id);

    // In fact, this method will not be called at runtime
    [Conditional("DI")]
    void Setup() =>

        DI.Setup()
            .Bind<IOrder>().To<Order>()
            .Bind<long>().To(GenerateId)
            // Binds the string with the tag "Order details"
            .Bind<string>("Order details").To(() => $"{storeName}_{GenerateId()}")
            .Root<OrderService>("OrderService", kind: Internal);
}
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 partial class is also useful for specifying access modifiers to the generated class.

The following partial class will be generated
partial class Composition
{
  internal OrderService OrderService
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      long transientInt64 = GenerateId();
      long transientInt641 = GenerateId();
      string transientString = $"{storeName}_{GenerateId()}";
      return new OrderService(transientString, new Order(transientInt64), new Order(transientInt641));
    }
  }
}

Class diagram:

---
 config:
  class:
   hideEmptyMembersBox: true
---
classDiagram
	Order --|> IOrder
	Composition ..> OrderService : OrderService OrderService
	Order *-- Int64 : Int64
	OrderService *-- "2 instances" Order : IOrder
	OrderService *-- String : "Order details" String
	namespace Pure.DI.UsageTests.Advanced.PartialClassScenario {
		class Composition {
		<<partial>>
		~OrderService OrderService
		}
		class IOrder {
			<<interface>>
		}
		class Order {
				<<class>>
			+Order(Int64 id)
		}
		class OrderService {
				<<class>>
			+OrderService(String details, IOrder order1, IOrder order2)
		}
	}
	namespace System {
		class Int64 {
				<<struct>>
		}
		class String {
				<<class>>
		}
	}
Loading

See also: