Skip to content

Latest commit

 

History

History
153 lines (129 loc) · 3.41 KB

File metadata and controls

153 lines (129 loc) · 3.41 KB

Partial class

A partial class can contain setup code.

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 transientInt6492 = GenerateId();
      long transientInt6493 = GenerateId();
      string transientString89 = $"{storeName}_{GenerateId()}";
      return new OrderService(transientString89, new Order(transientInt6492), new Order(transientInt6493));
    }
  }
}

Class diagram:

---
 config:
  maxTextSize: 2147483647
  maxEdges: 2147483647
  class:
   hideEmptyMembersBox: true
---
classDiagram
	Order --|> IOrder
	Composition ..> OrderService : OrderService OrderService
	Order *--  Int64 : Int64
	OrderService *-- "2 " 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