/// <summary>
 /// Handles the before add binding container event.
 /// 
 /// Used to ensure the binding value is a <see cref="UnityEngine.MonoBehaviour"/>.
 /// </summary>
 /// <param name="source">Source.</param>
 /// <param name="binding">Binding.</param>
 protected void OnBeforeAddBinding(IBinder source, ref BindingInfo binding)
 {
     if (binding.value is Type &&
         TypeUtils.IsAssignable(typeof(MonoBehaviour), binding.value as Type)) {
         throw new BindingException(CANNOT_RESOLVE_MONOBEHAVIOUR);
     }
 }
        /// <summary>
        /// handles the after add binding event.
        /// 
        /// Used to check whether singleton instances should be added to the updater.
        /// </summary>
        /// <param name="source">Source.</param>
        /// <param name="binding">Binding.</param>
        protected void OnAfterAddBinding(IBinder source, ref BindingInfo binding)
        {
            if (binding.instanceType == BindingInstance.Singleton) {
                //Do not add commands.
                if (binding.value is ICommand) return;

                if (binding.value is IDisposable) {
                    disposable.Add((IDisposable)binding.value);
                }
                if (binding.value is IUpdatable) {
                    updateable.Add((IUpdatable)binding.value);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Adds the binding to the internal dictionary.
        /// </summary>
        /// <param name="binding">The binding to be added.</param>
        protected void AddBindingToDictionary(BindingInfo binding)
        {
            if (this.beforeAddBinding != null)
            {
                this.beforeAddBinding(this, ref binding);
            }

            if (this.typeBindings.ContainsKey(binding.type))
            {
                this.typeBindings[binding.type].Add(binding);
            }
            else
            {
                var bindingList = new List <BindingInfo>(1);
                bindingList.Add(binding);
                this.typeBindings.Add(binding.type, bindingList);
            }

            if (this.afterAddBinding != null)
            {
                this.afterAddBinding(this, ref binding);
            }
        }
        /// <summary>
        /// Handles the binding evaluation container event.
        /// 
        /// Used to instantiate prefabs.
        /// </summary>
        /// <param name="source">Source.</param>
        /// <param name="binding">Binding.</param>
        protected object OnBindingEvaluation(IInjector source, ref BindingInfo binding)
        {
            //Checks whether a prefab should be instantiated.
            if (binding.value is PrefabBinding &&
                binding.instanceType == BindingInstance.Transient) {
                var prefabBinding = (PrefabBinding)binding.value;
                var gameObject = (GameObject)MonoBehaviour.Instantiate(prefabBinding.prefab);

                if (prefabBinding.type.Equals(typeof(GameObject))) {
                    return gameObject;
                } else {
                    var component = gameObject.GetComponent(prefabBinding.type);

                    if (component == null) {
                        component = gameObject.AddComponent(prefabBinding.type);
                    }

                    return component;
                }
            } else {
                return null;
            }
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Adic.Binding.SingleBindingConditionFactory"/> class.
 /// </summary>
 /// <param name="binding">The binding to have its conditions settled.</param>
 /// <param name="bindindCreator">Binding creator. Used for chaining.</param>
 public SingleBindingConditionFactory(BindingInfo binding, IBindingCreator bindindCreator)
 {
     this.binding        = binding;
     this.bindindCreator = bindindCreator;
 }
Example #6
0
 /// <summary>
 /// Resolves the binding provider.
 /// </summary>
 /// <param name="binding">Informatiion about the binding.</param>
 /// <returns>The binding provider.</returns>
 protected virtual IBindingConditionFactory CreateBindingConditionFactoryProvider(BindingInfo binding)
 {
     return(new SingleBindingConditionFactory(binding, this.binder));
 }
        /// <summary>
        /// Handles the binding resolution event.
        /// 
        /// Used to check whether the resolved instance should be added to the updater.
        /// </summary>
        /// <param name="source">Source.</param>
        /// <param name="binding">Binding.</param>
        /// <param name="instance">Instance.</param>
        protected void OnBindingResolution(IInjector source, ref BindingInfo binding, ref object instance)
        {
            //Do not add commands.
            if (binding.instanceType == BindingInstance.Singleton || instance is ICommand) return;

            if (instance is IDisposable) {
                disposable.Add((IDisposable)instance);
            }
            if (instance is IUpdatable) {
                updateable.Add((IUpdatable)instance);
            }
        }