Skip to content

About

A lightweight dependency injection framework for Unity using C# Source Generators.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

LazyInject

A lightweight dependency injection framework for Unity using C# Source Generators.

Features

  • Lazy Loading: Dependencies are resolved only when first accessed
  • Compile-time Generation: No runtime reflection overhead
  • Simple API: Just add [Inject] to your fields
  • Unity 6+ Compatible: Uses Roslyn Source Generators

Installation

Via Package Manager (Git URL)

  1. Open Window > Package Manager
  2. Click + > Add package from git URL
  3. Enter: https://github.com/rhysomo/LazyInject.git

Via manifest.json

Add to your Packages/manifest.json:

{
  "dependencies": {
    "dev.insd.lazyinject": "https://github.com/rhysomo/LazyInject.git"
  }
}

Usage

1. Register Dependencies

public class GameManager : MonoBehaviour
{
    void Awake()
    {
        // Register singleton services
        DIContainer.Global.Register<PlayerState>(new PlayerState());
        DIContainer.Global.Register<ILogger>(new GameLogger());

        // Register with key
        DIContainer.Global.Register<ILogger>(new DebugLogger(), "debug");
    }
}

2. Inject Dependencies

public partial class PlayerController : MonoBehaviour
{
    [Inject] private PlayerState _dPlayerState;
    [Inject("debug")] private ILogger _dLogger;

    void Start()
    {
        // Use generated properties (PascalCase of field name)
        DPlayerState.Initialize();
        DLogger.Log("Player initialized");
    }
}

Important Notes

  • Classes using [Inject] must be declared as partial
  • Access dependencies via generated Properties, not fields
  • Field accessibility is preserved (private field → private property)
  • Returns null if dependency is not registered (no exception thrown)
  • Recommended: Use d prefix in field names (e.g., _dPlayerState) to generate DPlayerState property and avoid confusion with type names

Generated Code Example

For this field:

[Inject] private PlayerState _dPlayerState;

The Source Generator creates:

private PlayerState DPlayerState
{
    get
    {
        if (_dPlayerState == null)
        {
            _dPlayerState = DIContainer.GetValue(typeof(PlayerState), "") as PlayerState;
        }
        return _dPlayerState;
    }
}

Diagnostics

Code Severity Description
LDI001 Error Direct access to [Inject] field is not allowed. Use the generated property instead.
LDI002 Warning Generated property name matches type name. Consider using d prefix to avoid confusion.

Building from Source

The Source Generator project is in the SourceGenerator~ folder (hidden from Unity).

Using the build script

./build.sh

This will build the DLL and copy it to the Plugins/ folder.

Manual build

cd SourceGenerator~
dotnet build -c Release
cp bin/Release/netstandard2.0/Inject.CodeGen.dll ../Plugins/

License

MIT License

About

A lightweight dependency injection framework for Unity using C# Source Generators.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages