示例#1
0
        void Register()
        {
            if (Installers.Where(x => x != null).IsEmpty())
            {
                Log.Warn("No installers found while initializing CompositionRoot");
                return;
            }

            foreach (var installer in Installers)
            {
                if (installer == null)
                {
                    Log.Warn("Found null installer hooked up to CompositionRoot");
                    continue;
                }

                if (installer.enabled)
                {
                    // The installers that are part of the scene are monobehaviours
                    // and therefore were not created via Zenject and therefore do
                    // not have their members injected
                    // At the very least they will need the container injected but
                    // they might also have some configuration passed from another
                    // scene as well
                    FieldsInjecter.Inject(_container, installer);
                    _container.Bind <IInstaller>().To(installer);

                    // Install this installer and also any other installers that it installs
                    _container.InstallInstallers();

                    Assert.That(!_container.HasBinding <IInstaller>());
                }
            }
        }
示例#2
0
        public DiContainer CreateContainer(bool allowNullBindings, DiContainer parentContainer)
        {
            var container = new DiContainer();

            container.AllowNullBindings = allowNullBindings;
            container.FallbackProvider  = new DiContainerProvider(parentContainer);

            // Install the extra bindings immediately in case they configure the
            // installers used in this scene
            if (ExtraBindingsLookup != null)
            {
                ExtraBindingsLookup(container);

                // Reset extra bindings for next time we change scenes
                ExtraBindingsLookup = null;
            }

            container.Bind <IInstaller>().ToSingle <StandardUnityInstaller>();
            container.Bind <GameObject>().To(this.gameObject).WhenInjectedInto <StandardUnityInstaller>();
            container.InstallInstallers();
            Assert.That(!container.HasBinding <IInstaller>());

            InstallSceneInstallers(container);

            return(container);
        }
示例#3
0
        static IEnumerable <ZenjectResolveException> ValidateInstallers(CompositionRoot compRoot)
        {
            var container = new DiContainer();

            container.Bind <CompositionRoot>().ToSingle(compRoot);

            var allInstallers = new List <IInstaller>();

            foreach (var installer in compRoot.Installers)
            {
                if (installer == null)
                {
                    yield return(new ZenjectResolveException(
                                     "Found null installer in properties of Composition Root"));

                    yield break;
                }

                if (installer.enabled)
                {
                    installer.Container = container;
                    container.Bind <IInstaller>().To(installer);
                }

                allInstallers.AddRange(container.InstallInstallers());

                Assert.That(!container.HasBinding <IInstaller>());
            }

            foreach (var error in container.ValidateResolve <IDependencyRoot>())
            {
                yield return(error);
            }

            // Also make sure we can fill in all the dependencies in the built-in scene
            foreach (var monoBehaviour in compRoot.GetComponentsInChildren <MonoBehaviour>())
            {
                if (monoBehaviour == null)
                {
                    // Be nice to give more information here
                    Log.Warn("Found null MonoBehaviour in scene");
                    continue;
                }

                foreach (var error in container.ValidateObjectGraph(monoBehaviour.GetType()))
                {
                    yield return(error);
                }
            }

            // Validate dynamically created object graphs
            foreach (var installer in allInstallers)
            {
                foreach (var error in installer.ValidateSubGraphs())
                {
                    yield return(error);
                }
            }
        }
示例#4
0
        void Resolve()
        {
            InjectionHelper.InjectChildGameObjects(_container, gameObject);

            if (_container.HasBinding <IDependencyRoot>())
            {
                _dependencyRoot = _container.Resolve <IDependencyRoot>();
                _dependencyRoot.Start();
            }
            else
            {
                Debug.LogWarning("No dependency root found");
            }
        }
示例#5
0
        public static DiContainer CreateContainer(bool allowNullBindings, GameObject gameObj)
        {
            Assert.That(allowNullBindings || gameObj != null);

            var container = new DiContainer();

            container.AllowNullBindings = allowNullBindings;

            container.Bind <IInstaller>().ToSingle <StandardUnityInstaller>();
            container.Bind <GameObject>().To(gameObj)
            .WhenInjectedInto <StandardUnityInstaller>();
            container.InstallInstallers();
            Assert.That(!container.HasBinding <IInstaller>());

            foreach (var installer in GetGlobalInstallers())
            {
                FieldsInjecter.Inject(container, installer);
                container.Bind <IInstaller>().To(installer);
                container.InstallInstallers();
                Assert.That(!container.HasBinding <IInstaller>());
            }

            return(container);
        }
示例#6
0
        object ResolveFromType(
            ResolveContext context, object injectable, InjectableInfo injectInfo)
        {
            if (_container.HasBinding(injectInfo.ContractType, context))
            {
                return(_container.Resolve(injectInfo.ContractType, context));
            }

            // If it's a list it might map to a collection
            if (ReflectionUtil.IsGenericList(injectInfo.ContractType))
            {
                var subType = injectInfo.ContractType.GetGenericArguments().Single();
                return(_container.ResolveMany(subType, context, injectInfo.Optional));
            }

            if (!injectInfo.Optional)
            {
                throw new ZenjectResolveException(
                          "Unable to find field with type '{0}' when injecting dependencies into '{1}'. \nObject graph:\n {2}"
                          .With(injectInfo.ContractType, injectable, _container.GetCurrentObjectGraph()));
            }

            return(null);
        }