hadashiA / VContainer

The extra fast, minimum code size, GC-free DI (Dependency Injection) library running on Unity Game Engine.
https://vcontainer.hadashikick.jp
MIT License
1.94k stars 169 forks source link

Parameter injection works only with methods for MonoBehaviour #432

Open nikolay-vrjam opened 2 years ago

nikolay-vrjam commented 2 years ago

Here is a MonoBehaviour with single parameter

public class VenueTestStarterController : MonoBehaviour, IStartable
    {
        public enum StartType
        {
            Active,
            Passive
        }

        [Inject] private StartType _startType; // This does NOT work

        // [Inject]  // This Works
        // private void Inject(StartType sType)
        // {
        //     _startType = sType;
        // }

        public void Start()
        {
            Debug.Log($"starttype={_startType}");
        }
    }

and this is a scope configuration

    protected override void Configure(IContainerBuilder builder)
        {
            builder.RegisterComponentOnNewGameObject<VenueTestStarterController>(Lifetime.Scoped,
                nameof(VenueTestStarterController)).WithParameter(VenueTestStarterController.StartType.Passive);
            builder.RegisterBuildCallback(resolver =>
            {
                resolver.Resolve<VenueTestStarterController>();
            });
        }

Injection only works when it happens through method (commented in the first snipped), otherwise, it shows following error

VContainerException: No such registration of type: Test.VenueTestStarter.VenueTestStarterController+StartType
VContainer.ScopedContainer.FindRegistration (System.Type type) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:181)
VContainer.ScopedContainer.Resolve (System.Type type) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:72)
VContainer.Internal.ReflectionInjector.InjectFields (System.Object obj, VContainer.IObjectResolver resolver) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Internal/ReflectionInjector.cs:66)
VContainer.Internal.ReflectionInjector.Inject (System.Object instance, VContainer.IObjectResolver resolver, System.Collections.Generic.IReadOnlyList`1[T] parameters) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Internal/ReflectionInjector.cs:26)
VContainer.Unity.NewGameObjectProvider.SpawnInstance (VContainer.IObjectResolver resolver) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Unity/InstanceProviders/NewGameObjectProvider.cs:44)
VContainer.Registration.SpawnInstance (VContainer.IObjectResolver resolver) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Registration.cs:33)
VContainer.ScopedContainer+<>c__DisplayClass14_0.<.ctor>b__1 () (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:67)
System.Lazy`1[T].ViaFactory (System.Threading.LazyThreadSafetyMode mode) (at <d4cde64232cf45659d86aafa597faa77>:0)
System.Lazy`1[T].ExecutionAndPublication (System.LazyHelper executionAndPublication, System.Boolean useDefaultConstructor) (at <d4cde64232cf45659d86aafa597faa77>:0)
System.Lazy`1[T].CreateValue () (at <d4cde64232cf45659d86aafa597faa77>:0)
System.Lazy`1[T].get_Value () (at <d4cde64232cf45659d86aafa597faa77>:0)
VContainer.ScopedContainer.CreateTrackedInstance (VContainer.Registration registration) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:138)
VContainer.ScopedContainer.ResolveCore (VContainer.Registration registration) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:125)
VContainer.ScopedContainer.Resolve (VContainer.Registration registration) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:81)
VContainer.ScopedContainer.Resolve (System.Type type) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:72)
VContainer.IObjectResolverExtensions.Resolve[T] (VContainer.IObjectResolver resolver) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/IObjectResolverExtensions.cs:10)
VRJCore.TestVenueStarterScope+<>c.<Configure>b__0_0 (VContainer.IObjectResolver resolver) (at Assets/Test/Scripts/VenueTestStarter/TestVenueStarterScope.cs:17)
VContainer.ContainerBuilder.EmitCallbacks (VContainer.IObjectResolver container) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/ContainerBuilder.cs:156)
VContainer.ScopedContainerBuilder.BuildScope () (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/ContainerBuilder.cs:41)
VContainer.ScopedContainer.CreateScope (System.Action`1[T] installation) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:89)
VContainer.Container.CreateScope (System.Action`1[T] installation) (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Container.cs:228)
VContainer.Unity.LifetimeScope.Build () (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Unity/LifetimeScope.cs:161)
VContainer.Unity.LifetimeScope.Awake () (at Library/PackageCache/jp.hadashikick.vcontainer@1.11.1/Runtime/Unity/LifetimeScope.cs:121)
mrcece commented 1 year ago

"you must use method injection to inject them with dependencies."
More info: njecting into MonoBehaviours

SimonNordon4 commented 1 year ago

Should .WithParameter() be exclusive to Construction Methods? Maybe there's an argument that they should be supported with all Injection types.