Exemple #1
0
        /// Creates and registers one or more Mediators for a specific View instance.
        /// Takes a specific View instance and a binding and, if a binding is found for that type, creates and registers a Mediator.
        virtual protected void mapView(IView view, IMediationBinding binding)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    MonoBehaviour mono         = view as MonoBehaviour;
                    Type          mediatorType = values[a] as Type;
                    if (mediatorType == viewType)
                    {
                        throw new MediationException(viewType + "mapped to itself. The result would be a stack overflow.", MediationExceptionType.MEDIATOR_VIEW_STACK_OVERFLOW);
                    }
                    MonoBehaviour mediator = mono.gameObject.AddComponent(mediatorType) as MonoBehaviour;
                    if (mediator is IMediator)
                    {
                        ((IMediator)mediator).PreRegister();
                    }
                    injectionBinder.Bind(viewType).ToValue(view).ToInject(false);
                    injectionBinder.injector.Inject(mediator);
                    injectionBinder.Unbind(viewType);
                    if (mediator is IMediator)
                    {
                        ((IMediator)mediator).OnRegister();
                    }
                }
            }
        }
Exemple #2
0
        public void Trigger(MediationEvent evt, IView view)
        {
            Type viewType             = view.GetType();
            IMediationBinding binding = GetBinding(viewType) as IMediationBinding;

            if (binding != null)
            {
                switch (evt)
                {
                case MediationEvent.AWAKE:
                    InjectViewAndChildren(view);
                    MapView(view, binding);
                    break;

                case MediationEvent.DESTROYED:
                    UnmapView(view, binding);
                    break;

                default:
                    break;
                }
            }
            else if (evt == MediationEvent.AWAKE)
            {
                //Even if not mapped, Views (and their children) have potential to be injected
                InjectViewAndChildren(view);
            }
        }
Exemple #3
0
        /// Creates and registers one or more Mediators for a specific View instance.
        /// Takes a specific View instance and a binding and, if a binding is found for that type, creates and registers a Mediator.
        virtual protected void mapView(IView view, IMediationBinding binding)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    MonoBehaviour mono = view as MonoBehaviour;

                    /*Type mediatorType = values[a] as Type;
                     * if (mediatorType == viewType)
                     * {
                     *  throw new MediationException(viewType + "mapped to itself. The result would be a stack overflow.", MediationExceptionType.MEDIATOR_VIEW_STACK_OVERFLOW);
                     * }
                     */
                    if (mono is IMediator)
                    {
                        ((IMediator)mono).PreRegister();
                    }

                    Type typeToInject = (binding.abstraction == null || binding.abstraction.Equals(BindingConst.NULLOID)) ? viewType : binding.abstraction as Type;
                    injectionBinder.Bind(typeToInject).ToValue(view).ToInject(false);
                    injectionBinder.injector.Inject(mono);
                    injectionBinder.Unbind(typeToInject);
                    if (mono is IMediator)
                    {
                        ((IMediator)mono).OnRegister();
                    }
                }
            }
        }
Exemple #4
0
        public void Trigger(MediationEvent evt, IView view)
        {
            Type viewType             = view.GetType();
            IMediationBinding binding = GetBinding(viewType) as IMediationBinding;

            if (binding != null)
            {
                switch (evt)
                {
                case MediationEvent.AWAKE:
                    MapView(view, binding);
                    break;

                case MediationEvent.DESTROYED:
                    UnmapView(view, binding);
                    break;

                case MediationEvent.ENABLED:
                    EnableView(view, binding);
                    break;

                case MediationEvent.DISABLED:
                    DisableView(view, binding);
                    break;

                default:
                    break;
                }
            }
            else
            {
                Debug.Log("Hello there, you are trying to bind " + view + " without a mediation. This is not allowed.");
            }
        }
        /// Creates and registers one or more Mediators for a specific View instance.
        /// Takes a specific View instance and a binding and, if a binding is found for that type, creates and registers a Mediator.
        virtual protected void mapView(IView view, IMediationBinding binding)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    MonoBehaviour mono         = view as MonoBehaviour;
                    Type          mediatorType = values [a] as Type;
                    if (mediatorType == viewType)
                    {
                        throw new MediationException(viewType + "mapped to itself. The result would be a stack overflow.", MediationExceptionType.MEDIATOR_VIEW_STACK_OVERFLOW);
                    }
                    MonoBehaviour mediator = mono.gameObject.AddComponent(mediatorType) as MonoBehaviour;
                    if (mediator == null)
                    {
                        throw new MediationException("The view: " + viewType.ToString() + " is mapped to mediator: " + mediatorType.ToString() + ". AddComponent resulted in null, which probably means " + mediatorType.ToString().Substring(mediatorType.ToString().LastIndexOf(".") + 1) + " is not a MonoBehaviour.", MediationExceptionType.NULL_MEDIATOR);
                    }
                    if (mediator is IMediator)
                    {
                        ((IMediator)mediator).PreRegister();
                    }
                    injectionBinder.Bind(viewType).ToValue(view).ToInject(false);
                    injectionBinder.injector.Inject(mediator);
                    injectionBinder.Unbind(viewType);
                    if (mediator is IMediator)
                    {
                        ((IMediator)mediator).OnRegister();
                    }
                }
            }
        }
        protected override void UnmapView(IView view, IMediationBinding binding)
        {
            base.UnmapView(view, binding);
            var mono = view as MonoBehaviour;

            HandleDelegates(view, mono.GetType(), false);
        }
Exemple #7
0
        /// Add a Mediator to a View. If the mediator is a "true" Mediator (i.e., it
        /// implements IMediator), perform PreRegister and OnRegister.
        protected virtual void ApplyMediationToView(IMediationBinding binding, IView view, Type mediatorType)
        {
            bool isTrueMediator = IsTrueMediator(mediatorType);

            if (!isTrueMediator || !HasMediator(view, mediatorType))
            {
                Type   viewType = view.GetType();
                object mediator = CreateMediator(view, mediatorType);

                if (mediator == null)
                {
                    ThrowNullMediatorError(viewType, mediatorType);
                }
                if (isTrueMediator)
                {
                    ((IMediator)mediator).PreRegister();
                }

                Type typeToInject = (binding.abstraction == null || binding.abstraction.Equals(BindingConst.NULLOID)) ? viewType : binding.abstraction as Type;
                injectionBinder.Bind(typeToInject).ToValue(view).ToInject(false);
                injectionBinder.injector.Inject(mediator);
                injectionBinder.Unbind(typeToInject);
                if (isTrueMediator)
                {
                    ((IMediator)mediator).OnRegister();
                }
            }
        }
Exemple #8
0
        /*
         * Private.
         */

        private bool TryCreateMediator(IMediationBinding binding, out IMediator mediator)
        {
            if (binding != null && binding.MediatorType != null)
            {
                mediator = (IMediator)Activator.CreateInstance(binding.MediatorType);
                return(true);
            }

            mediator = null;
            return(false);
        }
Exemple #9
0
        /// Removes a mediator when its view is destroyed
        virtual protected void UnmapView(IView view, IMediationBinding binding)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    Type mediatorType = values[a] as Type;
                    DestroyMediator(view, mediatorType);
                }
            }
        }
Exemple #10
0
        /// Triggers given function in all mediators bound to given view
        virtual protected void TriggerInBindings(IView view, IMediationBinding binding, Func <IView, Type, object> method)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    Type mediatorType = values[a] as Type;
                    method(view, mediatorType);
                }
            }
        }
Exemple #11
0
        protected override void mapBindings()
        {
            //Bind some abstract and/or concrete Views


            Debug.LogWarning("mapBindings: " + mediationBinder);

            IMediationBinding binding = mediationBinder.Bind <TesterView> ();

            Debug.LogWarning("binding: " + binding);

            binding = binding.ToAbstraction <ITesterView> ();

            Debug.LogWarning("binding2: " + binding);

            binding = binding.To <BoxCollider2D> ();
        }
Exemple #12
0
        /// Creates and registers one or more Mediators for a specific View instance.
        /// Takes a specific View instance and a binding and, if a binding is found for that type, creates and registers a Mediator.
        virtual protected void MapView(IView view, IMediationBinding binding)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    Type mediatorType = values [a] as Type;
                    if (mediatorType == viewType)
                    {
                        throw new MediationException(viewType + "mapped to itself. The result would be a stack overflow.", MediationExceptionType.MEDIATOR_VIEW_STACK_OVERFLOW);
                    }
                    ApplyMediationToView(binding, view, mediatorType);
                }
            }
        }
Exemple #13
0
        private void InjectViewAndDependencies(IMediationBinding binding, IMediator mediator, IView view, bool triggerPostConstructors)
        {
            var injectionType = binding.ViewInterface != null ? binding.ViewInterface : binding.ViewType;

            if (_bindingsCache.TryGetValue(injectionType, out var injectionBinding))
            {
                injectionBinding.SetValue(view);
                _injectionBinder.Bind(injectionBinding);
                _injectionBinder.Construct(mediator, triggerPostConstructors);
                _injectionBinder.Unbind(injectionBinding);
            }
            else
            {
                injectionBinding = (IInjectionBinding)_injectionBinder.Bind(injectionType).ToValue(view);
                _injectionBinder.Construct(mediator, triggerPostConstructors);
                _injectionBinder.Unbind(injectionType);
                _bindingsCache.Add(injectionType, injectionBinding);
            }
        }
        /// Removes a mediator when its hollyView is destroyed
        protected virtual void unmapActor(IHollywoodView actor, IMediationBinding binding)
        {
            var actorType = actor.GetType();

            if (bindings.ContainsKey(actorType))
            {
                int length = 0;

                length = directory[actor].Count;

                for (var i = 0; i < length; i++)
                {
                    directory[actor][i].OnRemove();
                }
            }
            //remove reference to IActor and IDirector instances to allow gc
            directory[actor].Clear();
            directory.Remove(actor);
        }
Exemple #15
0
        /// Creates and registers one or more Mediators for a specific View instance.
        /// Takes a specific View instance and a binding and, if a binding is found for that type, creates and registers a Mediator.
        virtual protected void mapView(IView view, IMediationBinding binding)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    MonoBehaviour mono         = view as MonoBehaviour;
                    Type          mediatorType = values [a] as Type;
                    if (mediatorType == viewType)
                    {
                        throw new MediationException(viewType + "mapped to itself. The result would be a stack overflow.", MediationExceptionType.MEDIATOR_VIEW_STACK_OVERFLOW);
                    }

                    if (mono.gameObject.GetComponent(mediatorType) != null)
                    {
                        // since awake can potentially be called multiple times, don't add the mediator component twice
                        return;
                    }
                    MonoBehaviour mediator = mono.gameObject.AddComponent(mediatorType) as MonoBehaviour;
                    if (mediator == null)
                    {
                        throw new MediationException("The view: " + viewType.ToString() + " is mapped to mediator: " + mediatorType.ToString() + ". AddComponent resulted in null, which probably means " + mediatorType.ToString().Substring(mediatorType.ToString().LastIndexOf(".") + 1) + " is not a MonoBehaviour.", MediationExceptionType.NULL_MEDIATOR);
                    }
                    if (mediator is IMediator)
                    {
                        ((IMediator)mediator).PreRegister();
                    }

                    Type typeToInject = (binding.abstraction == null || binding.abstraction.Equals(BindingConst.NULLOID)) ? viewType : binding.abstraction as Type;
                    injectionBinder.Bind(typeToInject).ToValue(view).ToInject(false);
                    injectionBinder.injector.Inject(mediator);
                    injectionBinder.Unbind(typeToInject);
                    if (mediator is IMediator)
                    {
                        ((IMediator)mediator).OnRegister();
                    }
                }
            }
        }
Exemple #16
0
        /// Removes a mediator when its view is destroyed
        virtual protected void unmapView(IView view, IMediationBinding binding)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    Type          mediatorType = values[a] as Type;
                    MonoBehaviour mono         = view as MonoBehaviour;
                    IMediator     mediator     = mono.GetComponent(mediatorType) as IMediator;
                    if (mediator != null)
                    {
                        mediator.OnRemove();
                    }
                }
            }
        }
Exemple #17
0
        /// Creates and registers a Mediator for a specific View instance.
        /// Takes a specific View instance and a binding and, if a binding is found for that type, creates and registers a Mediator.
        virtual protected void mapView(IView view, IMediationBinding binding)
        {
            Type viewType = view.GetType();

            if (bindings.ContainsKey(viewType))
            {
                object[] values = binding.value as object[];
                int      aa     = values.Length;
                for (int a = 0; a < aa; a++)
                {
                    MonoBehaviour mono         = view as MonoBehaviour;
                    Type          mediatorType = values [a] as Type;
                    IMediator     mediator     = mono.gameObject.AddComponent(mediatorType) as IMediator;
                    mediator.PreRegister();
                    injectionBinder.Bind(viewType).ToValue(view).ToInject(false);
                    injectionBinder.injector.Inject(mediator);
                    injectionBinder.Unbind(viewType);
                    mediator.OnRegister();
                }
            }
        }
Exemple #18
0
    public virtual IMediationBinding BindMediator <View, Mediator>()
    {
        IMediationBinding binding = null;

        try
        {
            binding = mediationBinder.GetBinding <View>() as IMediationBinding;
        }
        catch (InjectionException)
        {
            //Workaround
            //Couldn't find Binding, strange throws error. why? I don't know
            //No "ContainsBinding" method yet
        }

        if (binding != null)
        {
            return(binding);
        }

        return(mediationBinder.Bind <View>().To <Mediator>());
    }
Exemple #19
0
        override protected IBinding ConsumeItem(Dictionary <string, object> item, IBinding testBinding)
        {
            IBinding binding = base.ConsumeItem(item, testBinding);

            foreach (var i in item)
            {
                if (i.Key == "ToAbstraction")
                {
                    Type abstractionType = Type.GetType(i.Value as string);
                    IMediationBinding mediationBinding = (binding as IMediationBinding);
                    if (abstractionType == null)
                    {
                        throw new BinderException("A runtime abstraction in the MediationBinder returned a null Type. " + i.ToString(), BinderExceptionType.RUNTIME_NULL_VALUE);
                    }
                    if (mediationBinding == null)
                    {
                        throw new MediationException("During an attempt at runtime abstraction a MediationBinding could not be found. " + i.ToString(), MediationExceptionType.BINDING_RESOLVED_TO_NULL);
                    }

                    mediationBinding.ToAbstraction(abstractionType);
                }
            }
            return(binding);
        }
        /// Creates and registers one or more Director for a specific Actor instance.
        /// Please note that in this extansion's way of thinking, an Actor has one and only one Director.
        /// Takes a specific Actor instance and a binding and, if a binding is found for that type, creates and registers a Director.
        protected virtual void mapView(IHollywoodView hollyView, IMediationBinding binding)
        {
            var viewType = hollyView.GetType();

            if (bindings.ContainsKey(viewType))
            {
                var values = binding.value as object[];

                var aa = values.Length;
                for (var a = 0; a < aa; a++)
                {
                    var mono = hollyView as MonoBehaviour;

                    IDirector director = null;
                    if (values[a] is Type)
                    {
                        var directorType = values[a] as Type;
                        if (directorType == viewType)
                        {
                            throw new MediationException(
                                      viewType + "mapped to itself. The result would be a stack overflow.",
                                      MediationExceptionType.MEDIATOR_VIEW_STACK_OVERFLOW);
                        }

                        IInjectionBinding bindingTest = injectionBinder.GetBinding(directorType, null) as IInjectionBinding;

                        if (bindingTest == null)
                        {
                            //Default use case
                            injectionBinder.Bind <IDirector>().To(directorType);
                            director = injectionBinder.GetInstance <IDirector>();
                            injectionBinder.Unbind <IDirector>();
                        }
                        else
                        {
                            //Actor is bind to an Interface...
                            director = injectionBinder.GetInstance(directorType) as IDirector;
                        }
                    }
                    else
                    {
                        //Actor is bind to a concrete object instance
                        director = values[a] as IDirector;
                    }

                    if (director is IDirector)
                    {
                        director.PreRegister();
                    }

                    var typeToInject = binding.abstraction == null || binding.abstraction.Equals(BindingConst.NULLOID)
                        ? viewType
                        : binding.abstraction as Type;
                    injectionBinder.Bind(typeToInject).ToValue(hollyView).ToInject(false);
                    injectionBinder.injector.Inject(director, false);
                    injectionBinder.Unbind(typeToInject);

                    if (director is IDirector)
                    {
                        director.OnRegister(hollyView as IActor);
                        (hollyView as IActor).Director = director;
                    }

                    registerDirector(hollyView, director);
                }
            }
        }
Exemple #21
0
 /// Disables a mediator when its view is disabled
 virtual protected void DisableView(IView view, IMediationBinding binding)
 {
     TriggerInBindings(view, binding, DisableMediator);
 }
Exemple #22
0
 /// Removes a mediator when its view is destroyed
 virtual protected void UnmapView(IView view, IMediationBinding binding)
 {
     TriggerInBindings(view, binding, DestroyMediator);
 }