Swap a dependency from code

To replace a dependency in a test, or at runtime for a cheat or a mod, perform the following steps:

  1. Create a registry, and add it before every other provider with Injector.AddFirst.
  2. Add the replacement to the registry.
  3. When you no longer need the replacement, remove the registry with Injector.Remove.
RegistryProvider stubs = new();
Injector.AddFirst(stubs);                       // 1. Tried before the provider list
stubs.Add<IAnalytics>(new FakeAnalytics());     // 2. The replacement

MatchReport report = new();   // a plain class: its [Inject] IAnalytics member receives FakeAnalytics

Injector.Remove(stubs);                         // 3. The other providers answer again

Step 3 is optional in a test run in the Editor and in Play mode: Selvage removes a provider added during a Test Runner test when the test ends, and a provider added in Play mode when you exit Play mode. A test run on a player keeps what it added, so remove it in the test's TearDown.

A member that was injected before the swap keeps its dependency, unless it's Reactive. To give it the replacement, call Manual.Reinject.

Swap a class with an assembly

A class marked [Provide] in an assembly that only exists in some cases replaces the classes of the other assemblies in those cases. No code chooses between the classes: the assembly each one is in decides.

// In your game assembly
[Provide]
public class SteamStore : IStore { ... }

// In an Editor assembly: IStore members receive MockStore in the Editor, and SteamStore in a build.
[Provide]
public class MockStore : IStore { ... }

For a requested type, the classes of the highest-ranked assembly replace those of every lower rank:

RankAssemblyIts classes apply
1Test assemblyWhile a Test Runner run that includes the assembly is in progress.
2Editor assembly: an Editor folder, or an assembly definition limited to the Editor platformIn the Editor, in Edit mode and in Play mode.
3Assembly definition with platforms or define constraintsWhenever it's compiled.
4Project assembly, under AssetsAlways.
5Package assembly, under PackagesAlways.

The rank applies to the classes the Attribute Provider gives. The order of the provider list still comes first, so an object in the Registry Provider wins over any class. The Dependencies window names the class that gives the dependency and the class it replaces. The Validation tab reads the assemblies of the Editor and of the current build target, and updates when you switch the target.

Swap a class from a provider

To choose a class from code, write a provider that picks it, then add the provider to the provider list:

[Serializable]
public sealed class InputProvider : Provider
{
    protected override Verdict Evaluate(Request request)
        => (request.Type == typeof(IInput)) ? Verdict.Always() : Verdict.Never;

    protected override object ShareKey(Request request, object context, object channel)
        => typeof(IInput);

    protected override Supply Provide(Request request, object context, object channel)
        => Supply.Owned((Application.isMobilePlatform) ? new TouchInput() : new DesktopInput());
}

For more information, refer to Write a custom provider.