Hints are used to fine-tune code generation. The OnNewInstance hint determines whether to generate partial OnNewInstance method.
In addition, setup hints can be comments before the Setup method in the form hint = value, for example: // OnNewInstance = On.
using Shouldly;
using Pure.DI;
using static Pure.DI.Hint;
DI.Setup(nameof(Composition))
.Hint(OnNewInstance, "On")
.Hint(OnNewInstanceLifetimeRegularExpression, "(Singleton|PerBlock)")
.Bind().As(Lifetime.Singleton).To<GlobalCache>()
.Bind().As(Lifetime.PerBlock).To<OrderProcessor>()
.Root<IOrderProcessor>("OrderProcessor");
var log = new List<string>();
var composition = new Composition(log);
// Create the OrderProcessor twice
var processor1 = composition.OrderProcessor;
var processor2 = composition.OrderProcessor;
log.ShouldBe([
"GlobalCache created",
"OrderProcessor created",
"OrderProcessor created"
]);
interface IGlobalCache;
class GlobalCache : IGlobalCache;
interface IOrderProcessor
{
IGlobalCache Cache { get; }
}
class OrderProcessor(IGlobalCache cache) : IOrderProcessor
{
public IGlobalCache Cache { get; } = cache;
}
internal partial class Composition(List<string> log)
{
partial void OnNewInstance<T>(
ref T value,
object? tag,
Lifetime lifetime) =>
log.Add($"{typeof(T).Name} created");
}Running this code sample locally
- Make sure you have the .NET SDK 10.0 or later installed
dotnet --list-sdk- Create a net10.0 (or later) console application
dotnet new console -n Sampledotnet 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 runThe OnNewInstanceLifetimeRegularExpression hint helps you define a set of lifetimes that require instance creation control. You can use it to specify a regular expression to filter bindings by lifetime name.
For more hints, see this page.
The following partial class will be generated:
partial class Composition
{
#if NET9_0_OR_GREATER
private readonly Lock _lock = new Lock();
#else
private readonly Object _lock = new Object();
#endif
private GlobalCache? _singletonGlobalCache62;
public IOrderProcessor OrderProcessor
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (_singletonGlobalCache62 is null)
lock (_lock)
if (_singletonGlobalCache62 is null)
{
GlobalCache _singletonGlobalCache62Temp;
_singletonGlobalCache62Temp = new GlobalCache();
OnNewInstance<GlobalCache>(ref _singletonGlobalCache62Temp, null, Lifetime.Singleton);
Thread.MemoryBarrier();
_singletonGlobalCache62 = _singletonGlobalCache62Temp;
}
var perBlockOrderProcessor553 = new OrderProcessor(_singletonGlobalCache62);
OnNewInstance<OrderProcessor>(ref perBlockOrderProcessor553, null, Lifetime.PerBlock);
return perBlockOrderProcessor553;
}
}
partial void OnNewInstance<T>(ref T value, object? tag, Lifetime lifetime);
}Class diagram:
---
config:
maxTextSize: 2147483647
maxEdges: 2147483647
class:
hideEmptyMembersBox: true
---
classDiagram
GlobalCache --|> IGlobalCache
OrderProcessor --|> IOrderProcessor
Composition ..> OrderProcessor : IOrderProcessor OrderProcessor
OrderProcessor o-- "Singleton" GlobalCache : IGlobalCache
namespace Pure.DI.UsageTests.Hints.OnNewInstanceRegularExpressionHintScenario {
class Composition {
<<partial>>
+IOrderProcessor OrderProcessor
}
class GlobalCache {
<<class>>
+GlobalCache()
}
class IGlobalCache {
<<interface>>
}
class IOrderProcessor {
<<interface>>
}
class OrderProcessor {
<<class>>
+OrderProcessor(IGlobalCache cache)
}
}