Demonstrates how to use ref and out parameters in dependency injection for scenarios where you need to pass values by reference.
using Shouldly;
using Pure.DI;
DI.Setup("Composition")
// Represents a large data set or buffer
.Bind().To<int[]>(() => [10, 20, 30])
.Root<Service>("MyService");
var composition = new Composition();
var service = composition.MyService;
service.Sum.ShouldBe(60);
class Service
{
public int Sum { get; private set; }
// Ref structs cannot be fields, so they are injected via a method
// with the [Ordinal] attribute. This allows working with
// high-performance types like Span<T> or other ref structs.
[Ordinal]
public void Initialize(ref Data data) =>
Sum = data.Sum();
}
// A ref struct that holds a reference to the data
// to process it without additional memory allocations
readonly ref struct Data(ref int[] data)
{
private readonly ref int[] _dep = ref data;
public int Sum() => _dep.Sum();
}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 runNote
ref dependencies are useful for scenarios where you need to return multiple values or modify parameters during injection.
The following partial class will be generated:
partial class Composition
{
public Service MyService
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
int[] transient320 = [10, 20, 30];
int[] transient320_ref = transient320;
var transientService318 = new Service();
Data transientData319_ref = new Data(ref transient320_ref);
transientService318.Initialize(ref transientData319_ref);
return transientService318;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Resolve<T>()
{
return Resolver<T>.Value.Resolve(this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Resolve<T>(object? tag)
{
return Resolver<T>.Value.ResolveByTag(this, tag);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Resolve(Type type)
{
#if NETCOREAPP3_0_OR_GREATER
var index = (int)(_bucketSize * (((uint)type.TypeHandle.GetHashCode()) % 1));
#else
var index = (int)(_bucketSize * (((uint)RuntimeHelpers.GetHashCode(type)) % 1));
#endif
ref var pair = ref _buckets[index];
return Object.ReferenceEquals(pair.Key, type) ? pair.Value.Resolve(this) : Resolve(type, index);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private object Resolve(Type type, int index)
{
var finish = index + _bucketSize;
while (++index < finish)
{
ref var pair = ref _buckets[index];
if (Object.ReferenceEquals(pair.Key, type))
{
return pair.Value.Resolve(this);
}
}
throw new CannotResolveException($"{CannotResolveMessage} {OfTypeMessage} {type}.", type, null);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Resolve(Type type, object? tag)
{
#if NETCOREAPP3_0_OR_GREATER
var index = (int)(_bucketSize * (((uint)type.TypeHandle.GetHashCode()) % 1));
#else
var index = (int)(_bucketSize * (((uint)RuntimeHelpers.GetHashCode(type)) % 1));
#endif
ref var pair = ref _buckets[index];
return Object.ReferenceEquals(pair.Key, type) ? pair.Value.ResolveByTag(this, tag) : Resolve(type, tag, index);
}
[MethodImpl(MethodImplOptions.NoInlining)]
private object Resolve(Type type, object? tag, int index)
{
var finish = index + _bucketSize;
while (++index < finish)
{
ref var pair = ref _buckets[index];
if (Object.ReferenceEquals(pair.Key, type))
{
return pair.Value.ResolveByTag(this, tag);
}
}
throw new CannotResolveException($"{CannotResolveMessage} \"{tag}\" {OfTypeMessage} {type}.", type, tag);
}
private readonly static uint _bucketSize;
private readonly static Pair<IResolver<Composition, object>>[] _buckets;
static Composition()
{
var valResolver_0000 = new Resolver_0000();
Resolver<Service>.Value = valResolver_0000;
_buckets = Buckets<IResolver<Composition, object>>.Create(
1,
out _bucketSize,
new Pair<IResolver<Composition, object>>[1]
{
new Pair<IResolver<Composition, object>>(typeof(Service), valResolver_0000)
});
}
private const string CannotResolveMessage = "Cannot resolve composition root ";
private const string OfTypeMessage = "of type ";
private class Resolver<T>: IResolver<Composition, T>
{
public static IResolver<Composition, T> Value = new Resolver<T>();
public virtual T Resolve(Composition composite)
{
throw new CannotResolveException($"{CannotResolveMessage}{OfTypeMessage}{typeof(T)}.", typeof(T), null);
}
public virtual T ResolveByTag(Composition composite, object tag)
{
throw new CannotResolveException($"{CannotResolveMessage}\"{tag}\" {OfTypeMessage}{typeof(T)}.", typeof(T), tag);
}
}
private sealed class Resolver_0000: Resolver<Service>
{
public override Service Resolve(Composition composition)
{
return composition.MyService;
}
public override Service ResolveByTag(Composition composition, object tag)
{
switch (tag)
{
case null:
return composition.MyService;
default:
return base.ResolveByTag(composition, tag);
}
}
}
}Class diagram:
---
config:
maxTextSize: 2147483647
maxEdges: 2147483647
class:
hideEmptyMembersBox: true
---
classDiagram
Composition ..> Service : Service MyService
Service *-- Data : Data
Data *-- ArrayᐸInt32ᐳ : ArrayᐸInt32ᐳ
class ArrayᐸInt32ᐳ {
<<array>>
}
namespace Pure.DI.UsageTests.Basics.RefDependenciesScenario {
class Composition {
<<partial>>
+Service MyService
+ T ResolveᐸTᐳ()
+ T ResolveᐸTᐳ(object? tag)
+ object Resolve(Type type)
+ object Resolve(Type type, object? tag)
}
class Data {
<<struct>>
+Data(ArrayᐸInt32ᐳ data)
}
class Service {
<<class>>
+Service()
+Initialize(Data data) : Void
}
}