Table of Contents

Class Ctx<TService>

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

Experimental class to allow for overriding a service in a specific context. This is useful for testing or for scenarios where you want to temporarily replace a service with a different implementation.

[InternalApi_DoNotUse_MayChangeWithoutNotice]
public sealed class Ctx<TService> where TService : class

Type Parameters

TService
Inheritance
object
Ctx<TService>

Remarks

The service can be consumed anywhere, but will throw an error if no override was given (and IsSet is not checked first). You can also use ValueOptional to ignore not-found values.

Constructors

Ctx(ISysServiceProvider)

Experimental class to allow for overriding a service in a specific context. This is useful for testing or for scenarios where you want to temporarily replace a service with a different implementation.

public Ctx(ISysServiceProvider sp)

Parameters

sp ISysServiceProvider

Remarks

The service can be consumed anywhere, but will throw an error if no override was given (and IsSet is not checked first). You can also use ValueOptional to ignore not-found values.

Properties

IsSet

public bool IsSet { get; }

Property Value

bool

Value

The value as provided by the override.

public TService Value { get; }

Property Value

TService

Exceptions

CtxNotAssignedException

ValueOptional

The value - or null if it was not set. This is the only property that will not throw an exception if the value was not set.

public TService? ValueOptional { get; }

Property Value

TService

ValueOrNull

Can also return null - but will still throw exception if it was not overriden.

public TService? ValueOrNull { get; }

Property Value

TService

Methods

Use(Func<TService>, string?)

Register a replacement using a function - for delayed retrieval.

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

Parameters

valueFactory Func<TService>
cName string

Returns

IDisposable

Use(TService, string?)

Register a replacement for the context.

public static IDisposable Use(TService value, string? cName = null)

Parameters

value TService
cName string

Returns

IDisposable