unity-vcontainer

VContainer dependency injection expert specializing in IoC container configuration, lifecycle management, and Unity-optimized DI patterns. Masters dependency resolution, scoped containers, and testable architecture design. Use PROACTIVELY for VContainer setup, service registration, or SOLID principle implementation.

⭐2stars🔀0forks📁View on GitHub🕐Updated Dec 29, 2025

When & Why to Use This Skill

This Claude skill serves as a specialized expert for VContainer, a high-performance dependency injection (DI) framework tailored for Unity. It provides comprehensive guidance on IoC container configuration, lifecycle management, and the implementation of SOLID principles to build scalable, maintainable, and testable game architectures. By mastering dependency resolution and scoped containers, it helps developers move away from the Service Locator pattern toward a more robust, decoupled codebase.

Use Cases

  • Architecting Unity Projects: Designing a robust dependency graph using LifetimeScopes to manage service availability and resource cleanup across different game scenes.
  • Refactoring for Testability: Decoupling MonoBehaviour logic from concrete implementations by introducing interface-based service registration and constructor injection for easier unit testing with mocks.
  • Implementing SOLID Principles: Guiding the transition from tightly coupled code to a modular architecture where dependencies are explicitly defined and managed by the IoC container.
  • Advanced Lifecycle Management: Configuring specific object lifetimes (Singleton, Transient, or Scoped) to optimize memory usage and ensure correct state persistence throughout the game session.
nameunity-vcontainer
descriptionVContainer dependency injection expert specializing in IoC container configuration, lifecycle management, and Unity-optimized DI patterns. Masters dependency resolution, scoped containers, and testable architecture design. Use PROACTIVELY for VContainer setup, service registration, or SOLID principle implementation.
- csharp-plugincsharp-code-style

Unity VContainer - High-Performance DI for Unity

Overview

VContainer is a high-performance IoC container for Unity, providing dependency injection patterns for testable and maintainable code.

Core Topics:

  • Constructor and method injection
  • Service registration patterns (Singleton, Transient, Scoped)
  • LifetimeScope hierarchies
  • MonoBehaviour injection
  • Factory patterns with DI
  • Testing with mocks

Foundation Required: unity-csharp-fundamentals (TryGetComponent, FindAnyObjectByType, null-safe coding)

Learning Path: DI fundamentals → VContainer basics → Advanced patterns → Testing

Quick Start

using VContainer;
using VContainer.Unity;

// Define service interface
public interface IPlayerService
{
    void Initialize();
}

// Implement service
public class PlayerService : IPlayerService
{
    public void Initialize() => Debug.Log("Player initialized");
}

// Setup LifetimeScope
public class GameLifetimeScope : LifetimeScope
{
    protected override void Configure(IContainerBuilder builder)
    {
        builder.Register<IPlayerService, PlayerService>(Lifetime.Singleton);
        builder.RegisterComponentInHierarchy<PlayerController>();
    }
}

// Inject into MonoBehaviour
public class PlayerController : MonoBehaviour
{
    [Inject] private readonly IPlayerService mPlayerService;

    void Start() => mPlayerService.Initialize();
}

Key Concepts

Lifetime Scopes

  • Singleton: One instance per container
  • Transient: New instance every resolve
  • Scoped: One instance per scope

Injection Types

  • Constructor Injection: Preferred for required dependencies
  • Method Injection: For optional dependencies
  • Property/Field Injection: Use [Inject] attribute

Reference Documentation

See VContainer Best Practices for detailed patterns.

Best Practices

  1. Register interfaces: Loose coupling and testability
  2. Constructor injection first: Explicit dependencies
  3. Avoid Service Locator: Don't resolve in Update loops
  4. Test with mocks: Use ContainerBuilder in tests
  5. Clear hierarchies: Root → Scene → Local scopes