consumer-type.md
July 3, 2026 ยท View on GitHub
Consumer type
ConsumerType is used to get the consumer type of the given dependency. The use of ConsumerType is demonstrated on the example of Serilog library:
using Shouldly;
using Serilog.Core;
using Serilog.Events;
using Pure.DI;
using Serilog.Core;
Serilog.ILogger serilogLogger = new Serilog.LoggerConfiguration().CreateLogger();
var composition = new Composition(logger: serilogLogger);
var service = composition.Root;
interface IDependency;
class Dependency : IDependency
{
public Dependency(Serilog.ILogger log)
{
log.Information("Dependency created");
}
}
interface IService
{
IDependency Dependency { get; }
}
class Service : IService
{
public Service(
Serilog.ILogger log,
IDependency dependency)
{
Dependency = dependency;
log.Information("Service initialized");
}
public IDependency Dependency { get; }
}
partial class Composition
{
private void Setup() =>
DI.Setup(nameof(Composition))
.Arg<Serilog.ILogger>("logger", "from arg")
.Bind().To(ctx => {
ctx.Inject<Serilog.ILogger>("from arg", out var logger);
// Enriches the logger with the specific context of the consumer type.
// ctx.ConsumerType represents the type into which the dependency is being injected.
// This allows logs to be tagged with the name of the class that created them.
return logger.ForContext(ctx.ConsumerType);
})
.Bind().To<Dependency>()
.Bind().To<Service>()
.Root<IService>(nameof(Root));
}
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 Sample
- Add references to the NuGet packages
dotnet add package Pure.DI
dotnet add package Shouldly
dotnet add package Serilog.Core
dotnet add package Serilog.Events
- Copy the example code into the Program.cs file
You are ready to run the example ๐
dotnet run
Note
ConsumerType is useful for creating context-aware loggers or when you need to know which type is consuming a dependency.
The following partial class will be generated
partial class Composition
{
private readonly Serilog.ILogger _argLogger;
[OrdinalAttribute(128)]
public Composition(Serilog.ILogger logger)
{
_argLogger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public IService Root
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
Serilog.ILogger transientILogger1;
Serilog.ILogger localLogger = _argLogger;
// Enriches the logger with the specific context of the consumer type.
// ctx.ConsumerType represents the type into which the dependency is being injected.
// This allows logs to be tagged with the name of the class that created them.
transientILogger1 = localLogger.ForContext(typeof(Dependency));
Serilog.ILogger transientILogger;
Serilog.ILogger localLogger1 = _argLogger;
// Enriches the logger with the specific context of the consumer type.
// ctx.ConsumerType represents the type into which the dependency is being injected.
// This allows logs to be tagged with the name of the class that created them.
transientILogger = localLogger1.ForContext(typeof(Service));
return new Service(transientILogger, new Dependency(transientILogger1));
}
}
}
Class diagram:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
Dependency --|> IDependency
Service --|> IService
Composition ..> Service : IService Root
ILogger o-- ILogger : "from arg" Argument "logger"
Dependency *-- ILogger : ILogger
Service *-- ILogger : ILogger
Service *-- Dependency : IDependency
namespace Pure.DI.UsageTests.Basics.ConsumerTypeScenario {
class Composition {
<<partial>>
+IService Root
}
class Dependency {
<<class>>
+Dependency(ILogger log)
}
class IDependency {
<<interface>>
}
class IService {
<<interface>>
}
class Service {
<<class>>
+Service(ILogger log, IDependency dependency)
}
}
namespace Serilog {
class ILogger {
<<interface>>
}
}
See also: