Table of Contents

Class OverrideService<TService>

Namespace
ToSic.Sys.DI
Assembly
ToSic.Sys.Core.dll

Dependency Injection helper to override a service type with a different implementation.

[InternalApi_DoNotUse_MayChangeWithoutNotice]
public static class OverrideService<TService> where TService : class

Type Parameters

TService
Inheritance
object
OverrideService<TService>

Remarks

When using (...) this, it will ensure that any code within the using will receive the overridden implementation, while code outside will receive the original implementation.

This will only have an effect, if a service was initially registered for overriding, using the various Register<TImplementation>() methods.

Properties

CurrentFactory

Gets the current active factory delegate.

public static Func<IServiceProvider, TService>? CurrentFactory { get; }

Property Value

Func<IServiceProvider, TService>

IsOverridden

Indicates whether an override is active in the current execution context.

public static bool IsOverridden { get; }

Property Value

bool

OverrideCount

Gets the total number of nested overrides currently active (0 if none).

public static int OverrideCount { get; }

Property Value

int

OverridePath

public static string OverridePath { get; }

Property Value

string

Methods

Register()

Generate the factory function to either get from the override, or from the underlying service provider.

public static Func<IServiceProvider, TService> Register()

Returns

Func<IServiceProvider, TService>

A factory function to create the service instance

Register(Func<IServiceProvider, TService>)

Register with a custom factory method

public static Func<IServiceProvider, TService> Register(Func<IServiceProvider, TService> factory)

Parameters

factory Func<IServiceProvider, TService>

A factory function to create the service instance

Returns

Func<IServiceProvider, TService>

A factory function to create the service instance

Register(Func<TService>)

Register with a custom factory method

public static Func<IServiceProvider, TService> Register(Func<TService> factory)

Parameters

factory Func<TService>

A factory function to create the service instance

Returns

Func<IServiceProvider, TService>

A factory function to create the service instance

RegisterScoped()

public static Func<IServiceProvider, TService> RegisterScoped()

Returns

Func<IServiceProvider, TService>

RegisterScoped<TImplementation>()

Register as if it were scoped. We need this, because the true registration must be transient (otherwise the factory would never be rechecked). But we still want to be able to have the initial registration behave as scoped, which is why we do this.

public static Func<IServiceProvider, TService> RegisterScoped<TImplementation>() where TImplementation : class, TService

Returns

Func<IServiceProvider, TService>

Type Parameters

TImplementation

Register<TImplementation>()

Generate the factory function to either get from the override, or from the underlying service provider.

public static Func<IServiceProvider, TService> Register<TImplementation>() where TImplementation : class, TService

Returns

Func<IServiceProvider, TService>

A factory function to create the service instance

Type Parameters

TImplementation

Implementation type

Use(Func<IServiceProvider, TService>, string?, string?)

Use Overload 3: Supply a custom factory function

public static IDisposable Use(Func<IServiceProvider, TService> factory, string? hint = null, string? cName = null)

Parameters

factory Func<IServiceProvider, TService>

The factory function to use.

hint string

An optional hint for debugging purposes.

cName string

The caller member name (automatic)

Returns

IDisposable

UseIfNotOverridden(Func<IServiceProvider, TService>, string?, string?)

public static IDisposable UseIfNotOverridden(Func<IServiceProvider, TService> factory, string? hint = null, string? cName = null)

Parameters

factory Func<IServiceProvider, TService>
hint string
cName string

Returns

IDisposable

UseIfNotOverridden<TImplementation>(string?, string?)

Idempotent Begin: Only applies if no override exists

public static IDisposable UseIfNotOverridden<TImplementation>(string? hint = null, string? cName = null) where TImplementation : class, TService

Parameters

hint string
cName string

The caller member name (automatic)

Returns

IDisposable

Type Parameters

TImplementation

Use<TImplementation>(string?, string?)

Use Overload 1: Supply a type (preserves transient lifetime via DI container)

public static IDisposable Use<TImplementation>(string? hint = null, string? cName = null) where TImplementation : class, TService

Parameters

hint string

An optional hint for debugging purposes.

cName string

The caller member name (automatic)

Returns

IDisposable

Type Parameters

TImplementation

The type to resolve instead, must be registered with the DI container.

Use<TImplementation>(TImplementation, string?, string?)

Use Overload 2: Supply a specific instance (singleton)

public static IDisposable Use<TImplementation>(TImplementation value, string? hint = null, string? cName = null) where TImplementation : class, TService

Parameters

value TImplementation

The specific instance to use.

hint string

An optional hint for debugging purposes.

cName string

The caller member name (automatic)

Returns

IDisposable

Type Parameters

TImplementation

The type of the instance to use.