Пример #1
0
        void InstallZenjectBinding(ZenjectBinding binding)
        {
            if (!binding.enabled)
            {
                return;
            }

            if (binding.Components == null || binding.Components.IsEmpty())
            {
                Log.Warn("Found empty list of components on ZenjectBinding on object '{0}'", binding.name);
                return;
            }

            string identifier = null;

            if (binding.Identifier.Trim().Length > 0)
            {
                identifier = binding.Identifier;
            }

            foreach (var component in binding.Components)
            {
                var bindType = binding.BindType;

                if (component == null)
                {
                    Log.Warn("Found null component in ZenjectBinding on object '{0}'", binding.name);
                    continue;
                }

                var componentType = component.GetType();

                switch (bindType)
                {
                case ZenjectBinding.BindTypes.Self:
                {
                    Container.Bind(componentType).WithId(identifier).FromInstance(component);
                    break;
                }

                case ZenjectBinding.BindTypes.BaseType:
                {
                    Container.Bind(componentType.BaseType()).WithId(identifier).FromInstance(component);
                    break;
                }

                case ZenjectBinding.BindTypes.AllInterfaces:
                {
                    Container.Bind(componentType.Interfaces()).WithId(identifier).FromInstance(component);
                    break;
                }

                case ZenjectBinding.BindTypes.AllInterfacesAndSelf:
                {
                    Container.Bind(componentType.Interfaces().Concat(new[] { componentType }).ToArray()).WithId(identifier).FromInstance(component);
                    break;
                }

                default:
                {
                    throw Assert.CreateException();
                }
                }
            }
        }
Пример #2
0
        public void InstallBinding(ZenjectBinding binding)
        {
            if (!binding.enabled)
            {
                return;
            }

            if (binding.Components == null || binding.Components.IsEmpty())
            {
                Log.Warn("Found empty list of components on ZenjectBinding on object '{0}'", binding.name);
                return;
            }

            string identifier = null;

            if (binding.Identifier.Trim().Length > 0)
            {
                identifier = binding.Identifier;
            }

            foreach (var component in binding.Components)
            {
                var bindType = binding.BindType;

                if (component == null)
                {
                    Log.Warn("Found null component in ZenjectBinding on object '{0}'", binding.name);
                    continue;
                }

                var componentType = component.GetType();

                ComponentInfo componentInfo;

                if (_componentMap.TryGetValue(component, out componentInfo))
                {
                    switch (bindType)
                    {
                    case ZenjectBinding.BindTypes.Self:
                    {
                        InstallSingleBinding(componentType, identifier, componentInfo);
                        break;
                    }

                    case ZenjectBinding.BindTypes.BaseType:
                    {
                        InstallSingleBinding(componentType.BaseType(), identifier, componentInfo);
                        break;
                    }

                    case ZenjectBinding.BindTypes.AllInterfaces:
                    {
                        foreach (var baseType in componentType.Interfaces())
                        {
                            InstallSingleBinding(baseType, identifier, componentInfo);
                        }

                        break;
                    }

                    case ZenjectBinding.BindTypes.AllInterfacesAndSelf:
                    {
                        foreach (var baseType in componentType.Interfaces())
                        {
                            InstallSingleBinding(baseType, identifier, componentInfo);
                        }
                        InstallSingleBinding(componentType, identifier, componentInfo);

                        break;
                    }

                    default:
                    {
                        throw Assert.CreateException();
                    }
                    }
                }
                else
                {
                    // In this case, we are adding a binding for a component that does not exist
                    // in our 'context'.  So we are not responsible for injecting it - there is
                    // another InitialComponentsInjecter that does this.
                    // Best we can do here is just add the instance to our container
                    // This may result in the instance being injected somewhere without itself
                    // being injected, but there's not much we can do about that
                    InstallNonInjectedBinding(bindType, identifier, component);
                }
            }
        }
        void UninstallZenjectBinding(ZenjectBinding binding)
        {
            if (!binding.enabled)
            {
                return;
            }

            if (binding.Components == null)
            {
                return;
            }

            string identifier = null;

            if (binding.Identifier.Trim().Length > 0)
            {
                identifier = binding.Identifier;
            }

            DiContainer container = _context.Container;

            foreach (var component in binding.Components)
            {
                var bindType = binding.BindType;

                if (component == null)
                {
                    continue;
                }

                var componentType = component.GetType();

                switch (bindType)
                {
                case ZenjectBinding.BindTypes.Self:
                {
                    if (identifier != null)
                    {
                        container.UnbindId(componentType, identifier);
                    }
                    else
                    {
                        container.Unbind(componentType);
                    }
                    break;
                }

                case ZenjectBinding.BindTypes.BaseType:
                {
                    if (identifier != null)
                    {
                        container.UnbindId(componentType.BaseType(), identifier);
                    }
                    else
                    {
                        container.Unbind(componentType.BaseType());
                    }
                    break;
                }

                case ZenjectBinding.BindTypes.AllInterfaces:
                {
                    if (identifier != null)
                    {
                        foreach (var interfaceType in componentType.Interfaces())
                        {
                            container.UnbindId(interfaceType, identifier);
                        }
                    }
                    else
                    {
                        foreach (var interfaceType in componentType.Interfaces())
                        {
                            container.Unbind(interfaceType);
                        }
                    }
                    break;
                }

                case ZenjectBinding.BindTypes.AllInterfacesAndSelf:
                {
                    if (identifier != null)
                    {
                        foreach (var interfaceType in componentType.Interfaces().Append(componentType))
                        {
                            container.UnbindId(interfaceType, identifier);
                        }
                    }
                    else
                    {
                        foreach (var interfaceType in componentType.Interfaces().Append(componentType))
                        {
                            container.Unbind(interfaceType);
                        }
                    }
                    break;
                }
                }
            }
        }
 void InstallZenjectBinding(ZenjectBinding binding)
 {
     _contextInstallZenjectBindingMethod.Invoke(_context, new object[] { binding });
 }
Пример #5
0
        public void InstallBinding(ZenjectBinding binding)
        {
            if (!binding.enabled)
            {
                return;
            }

            if (binding.Components == null || binding.Components.IsEmpty())
            {
                Log.Warn("Found empty list of components on ZenjectBinding on object '{0}'", binding.name);
                return;
            }

            string identifier = null;

            if (binding.Identifier.Trim().Length > 0)
            {
                identifier = binding.Identifier;
            }

            foreach (var component in binding.Components)
            {
                var bindType = binding.BindType;

                if (component == null)
                {
                    Log.Warn("Found null component in ZenjectBinding on object '{0}'", binding.name);
                    continue;
                }

                var componentType = component.GetType();

                ComponentInfo componentInfo;

                if (_componentMap.TryGetValue(component, out componentInfo))
                {
                    switch (bindType)
                    {
                        case ZenjectBinding.BindTypes.Self:
                        {
                            InstallSingleBinding(componentType, identifier, componentInfo);
                            break;
                        }
                        case ZenjectBinding.BindTypes.AllInterfaces:
                        {
                            foreach (var baseType in componentType.Interfaces())
                            {
                                InstallSingleBinding(baseType, identifier, componentInfo);
                            }

                            break;
                        }
                        case ZenjectBinding.BindTypes.AllInterfacesAndSelf:
                        {
                            foreach (var baseType in componentType.Interfaces())
                            {
                                InstallSingleBinding(baseType, identifier, componentInfo);
                            }
                            InstallSingleBinding(componentType, identifier, componentInfo);

                            break;
                        }
                        default:
                        {
                            throw Assert.CreateException();
                        }
                    }
                }
                else
                {
                    // In this case, we are adding a binding for a component that does not exist
                    // in our 'context'.  So we are not responsible for injecting it - there is
                    // another InitialComponentsInjecter that does this.
                    // Best we can do here is just add the instance to our container
                    // This may result in the instance being injected somewhere without itself
                    // being injected, but there's not much we can do about that
                    InstallNonInjectedBinding(bindType, identifier, component);
                }
            }
        }
Пример #6
0
 void InstallNonInjectedBinding(ZenjectBinding.BindTypes bindType, string identifier, Component component)
 {
     switch (bindType)
     {
         case ZenjectBinding.BindTypes.Self:
         {
             _container.Bind(component.GetType()).WithId(identifier).FromInstance(component, true);
             break;
         }
         case ZenjectBinding.BindTypes.AllInterfaces:
         {
             _container.BindAllInterfaces(component.GetType()).WithId(identifier).FromInstance(component, true);
             break;
         }
         case ZenjectBinding.BindTypes.AllInterfacesAndSelf:
         {
             _container.BindAllInterfacesAndSelf(component.GetType()).WithId(identifier).FromInstance(component, true);
             break;
         }
         default:
         {
             throw Assert.CreateException();
         }
     }
 }