Skip to content

Latest commit

 

History

History
167 lines (139 loc) · 3.57 KB

partial-class.md

File metadata and controls

167 lines (139 loc) · 3.57 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("Abc");
var service = composition.Root;

service.Name.ShouldBe("Abc_1");
service.Dependency1.Id.ShouldBe(2);
service.Dependency2.Id.ShouldBe(3);

interface IDependency
{
    long Id { get; }
}

class Dependency(long id) : IDependency
{
    public long Id { get; } = id;
}

class Service(
    [Tag("name with id")] string name,
    IDependency dependency1,
    IDependency dependency2)
{
    public string Name { get; } = name;

    public IDependency Dependency1 { get; } = dependency1;

    public IDependency Dependency2 { get; } = dependency2;
}

// The partial class is also useful for specifying access modifiers to the generated class
public partial class Composition
{
    private readonly string _serviceName = "";
    private long _id;

    // Customizable constructor
    public Composition(string serviceName)
        : this()
    {
        _serviceName = serviceName;
    }

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

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

        DI.Setup()
            .Bind<IDependency>().To<Dependency>()
            .Bind<long>().To(_ => GenerateId())
            .Bind<string>("name with id").To(
                _ => $"{_serviceName}_{GenerateId()}")
            .Root<Service>("Root", kind: Internal);
}
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 partial class is also useful for specifying access modifiers to the generated class.

The following partial class will be generated:

partial class Composition
{
  private readonly Composition _root;

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

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

  internal Service Root
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    get
    {
      string transientString1 = $"{_serviceName}_{GenerateId()}";
      long transientInt645 = GenerateId();
      long transientInt644 = GenerateId();
      return new Service(transientString1, new Dependency(transientInt645), new Dependency(transientInt644));
    }
  }
}

Class diagram:

---
 config:
  class:
   hideEmptyMembersBox: true
---
classDiagram
	Dependency --|> IDependency
	Composition ..> Service : Service Root
	Dependency *--  Int64 : Int64
	Service *-- "2 " Dependency : IDependency
	Service *--  String : "name with id"  String
	namespace Pure.DI.UsageTests.Advanced.PartialClassScenario {
		class Composition {
		<<partial>>
		~Service Root
		}
		class Dependency {
			+Dependency(Int64 id)
		}
		class IDependency {
			<<interface>>
		}
		class Service {
			+Service(String name, IDependency dependency1, IDependency dependency2)
		}
	}
	namespace System {
		class Int64 {
				<<struct>>
		}
		class String {
		}
	}
Loading