Search Results for

    Show / Hide Table of Contents

    🏭 Factory Service

    The FactoryService is responsible for applying dependency injection or post-construction logic to objects using dynamically discovered IPostProcessor implementations.

    It also provides an object pooling system to efficiently reuse objects while still running dependency injection on them.

    This allows you to keep your architecture clean, decoupled, and highly performant.


    ✅ Features

    • Scans all assemblies for classes that implement IPostProcessor
    • Instantiates and stores all valid processors at runtime
    • Allows you to inject or eject dependencies on any object
    • Provides a Unity-like Instantiate API that runs dependency injection automatically
    • Includes a Pooling System for optimized spawning and destruction

    📦 Instantiation & Pooling

    1. Regular Instantiation (with DI)

    When you instantiate an object, dependencies are automatically injected:

    var enemy = Services.Get<FactoryService>().Instantiate(enemyPrefab, position, rotation);
    

    This behaves like Unity's Object.Instantiate, but adds automatic injection.


    2. Instantiation From Pool

    Instead of creating new objects every time, you can spawn them from a pool:

    var bullet = Services.Get<FactoryService>().InstantiateFromPool(bulletPrefab, position, rotation);
    
    • Objects are reused instead of destroyed and recreated.
    • All dependencies are automatically injected on spawn.
    • On destruction, pooled objects are returned to the pool.

    3. Destroying Objects

    FactoryService automatically knows if an object was spawned from a pool:

    Services.Get<FactoryService>().Destroy(bullet);
    
    • If the object came from a pool → it's despawned and reusable.
    • If not → it's destroyed normally with Object.Destroy.

    🧪 Example Usage

    Inject dependencies manually into an object:

    Services.Get<FactoryService>().InjectDependency(myObject);
    

    Or spawn with pooling:

    var obj = Services.Get<FactoryService>().InstantiateFromPool(prefab, position, rotation);
    

    🛠️ Custom Post Processors

    The FactoryService applies logic using pluggable PostProcessors.

    Example:

    public class SamplePostProcessor : IPostProcessor
    {
        public void Process(object target)
        {
            if (target is SampleClass sampleObject)
            {
                //Do something with sampleObject
            }
        }
    
        public void Revert(object target)
        {
            if (target is SampleClass sampleObject)
            {
                //Revert changes on sampleObject
            }
        }
    }
    
    • No manual registration required.
    • Automatically discovered at runtime.
    • Used both for injection and ejection on destroy/despawn.

    🏆 Best Practices

    • Use pooling for short-lived, frequently spawned objects (bullets, particles, effects).
    • Use post processors to inject services instead of direct references.
    • Always destroy objects with FactoryService.Destroy() to ensure cleanup.
    • Decouple your architecture by relying on interfaces and injection.

    In This Article
    Back to top Ultimate Base Project