Skip to content

ServiceLocator

ServiceLocator is a static facade for resolving registered services by type at runtime. It is connected automatically when the DI container (AchEngineScope) is initialized.

API

csharp
namespace AchEngine.DI
{
    public static class ServiceLocator
    {
        // Whether the container is ready
        public static bool IsReady { get; }

        // Resolve a service (throws InvalidOperationException if missing)
        public static T Resolve<T>();

        // Try to resolve safely (returns false if missing)
        public static bool TryResolve<T>(out T result);
    }
}

Usage Example

csharp
// Basic lookup
var ui = ServiceLocator.Resolve<IUIService>();
ui.Show<MainMenuView>();

// Safe lookup
if (ServiceLocator.TryResolve<IAudioService>(out var audio))
{
    audio.PlayBGM("main_theme");
}

// Readiness check
if (!ServiceLocator.IsReady)
{
    Debug.LogWarning("The service container has not been initialized yet.");
    return;
}

[Inject] vs ServiceLocator

[Inject]ServiceLocator
Requires VContainer
Usage locationObjects created by the DI containerAnywhere
Recommended forTypical services and viewsMonoBehaviour, scene transitions
TestabilityHighMedium

Recommended Pattern

Use [Inject] whenever possible, and reserve ServiceLocator for objects that are not created directly by the DI container, such as MonoBehaviour.

Manual Setup (Without VContainer)

If you want to use ServiceLocator without VContainer, set up a resolver manually.

csharp
// Bootstrap code
var container = new Dictionary<Type, object>();
container[typeof(IGameService)] = new GameService();

ServiceLocator.Setup(type =>
{
    container.TryGetValue(type, out var obj);
    return obj;
});

MIT License