Skip to content

Latest commit

 

History

History
135 lines (110 loc) · 3.28 KB

generic-roots.md

File metadata and controls

135 lines (110 loc) · 3.28 KB

Generic roots

using Pure.DI;

DI.Setup(nameof(Composition))
    // This hint indicates to not generate methods such as Resolve
    .Hint(Hint.Resolve, "Off")
    .Bind().To<Dependency<TT>>()
    .Bind().To<Service<TT>>()
    // Creates OtherService manually,
    // just for the sake of example
    .Bind<OtherService<TT>>().To(ctx =>
    {
        ctx.Inject(out IDependency<TT> dependency);
        return new OtherService<TT>(dependency);
    })

    // Specifies to define composition roots for all types inherited from IService<TT>
    // available at compile time at the point where the method is called
    .Roots<IService<TT>>("GetMy{type}");

var composition = new Composition();

// service = new Service<int>(new Dependency<int>());
var service = composition.GetMyService_T<int>();

// someOtherService = new OtherService<int>(new Dependency<int>());
var someOtherService = composition.GetMyOtherService_T<string>();

interface IDependency<T>;

class Dependency<T> : IDependency<T>;

interface IService<T>;

class Service<T>(IDependency<T> dependency) : IService<T>;

class OtherService<T>(IDependency<T> dependency) : IService<T>;
Running this code sample locally
dotnet --list-sdk
  • Create a net9.0 (or later) console application
dotnet new console -n Sample
  • Add reference to NuGet package
dotnet add package Pure.DI
  • Copy the example code into the Program.cs file

You are ready to run the example 🚀

dotnet run

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;
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public OtherService<T3> GetMyOtherService_T<T3>()
  {
    OtherService<T3> transientOtherService0;
    IDependency<T3> localDependency97 = new Dependency<T3>();
    transientOtherService0 = new OtherService<T3>(localDependency97);
    return transientOtherService0;
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public Service<T3> GetMyService_T<T3>()
  {
    return new Service<T3>(new Dependency<T3>());
  }
}

Class diagram:

---
 config:
  class:
   hideEmptyMembersBox: true
---
classDiagram
	DependencyᐸT3ᐳ --|> IDependencyᐸT3ᐳ
	Composition ..> OtherServiceᐸT3ᐳ : OtherServiceᐸT3ᐳ GetMyOtherService_TᐸT3ᐳ()
	Composition ..> ServiceᐸT3ᐳ : ServiceᐸT3ᐳ GetMyService_TᐸT3ᐳ()
	OtherServiceᐸT3ᐳ *--  DependencyᐸT3ᐳ : IDependencyᐸT3ᐳ
	ServiceᐸT3ᐳ *--  DependencyᐸT3ᐳ : IDependencyᐸT3ᐳ
	namespace Pure.DI.UsageTests.Generics.GenericsRootsScenario {
		class Composition {
		<<partial>>
		+OtherServiceᐸT3ᐳ GetMyOtherService_TᐸT3ᐳ()
		+ServiceᐸT3ᐳ GetMyService_TᐸT3ᐳ()
		}
		class DependencyᐸT3ᐳ {
			+Dependency()
		}
		class IDependencyᐸT3ᐳ {
			<<interface>>
		}
		class OtherServiceᐸT3ᐳ {
		}
		class ServiceᐸT3ᐳ {
		}
	}
Loading