Hints are used to fine-tune code generation. The Resolve hint determines whether to generate Resolve methods. By default a set of four Resolve methods are generated. Set this hint to Off to disable the generation of resolve methods. This will reduce class composition generation time and no private composition roots will be generated in this case. When the Resolve hint is disabled, only the public root properties are available, so be sure to define them explicitly with the Root<T>(...)
method.
In addition, setup hints can be comments before the Setup method in the form hint = value
, for example: // Resolve = Off
.
using Pure.DI;
using static Pure.DI.Hint;
DI.Setup(nameof(Composition))
.Hint(Resolve, "Off")
.Bind().To<Dependency>()
.Root<IDependency>("DependencyRoot")
.Bind().To<Service>()
.Root<IService>("Root");
var composition = new Composition();
var service = composition.Root;
var dependencyRoot = composition.DependencyRoot;
interface IDependency;
class Dependency : IDependency;
interface IService;
class Service(IDependency dependency) : IService;
Running this code sample locally
- Make sure you have the .NET SDK 9.0 or later is installed
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
For more hints, see this page.
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;
}
public IDependency DependencyRoot
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return new Dependency();
}
}
public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return new Service(new Dependency());
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Service --|> IService
Dependency --|> IDependency
Composition ..> Service : IService Root
Composition ..> Dependency : IDependency DependencyRoot
Service *-- Dependency : IDependency
namespace Pure.DI.UsageTests.Hints.ResolveHintScenario {
class Composition {
<<partial>>
+IDependency DependencyRoot
+IService Root
}
class Dependency {
+Dependency()
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
+Service(IDependency dependency)
}
}