Пример #1
0
        /// <summary>
        /// Creates an instance of the object of the type created by the factory.
        /// </summary>
        /// <param name="context">Injection context.</param>
        /// <returns>The instance.</returns>
        public object Create(InjectionContext context)
        {
            //Resolve a cube.
            var cube = this.container.Resolve<Cube>();

            //Add the "Rotator" behaviour to the cube and sets its speed.
            //This script could already be in the prefab. It's added here
            //only to show that factories can be used to fully configure
            //any object they create.
            var rotator = cube.gameObject.AddComponent<Rotator>();
            rotator.speed = Random.Range(0.05f, 5.0f);

            //Set the cube's color.
            cube.color = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f));

            //Set its position in the matrix.
            var transform = cube.GetComponent<Transform>();
            transform.position = new Vector3(1.5f * this.currentColumn++, -1.5f * this.currentLine, 0);

            //Check for line break.
            if (this.currentColumn >= MAX_COLUMNS) {
                this.currentLine++;
                this.currentColumn = 0;
            }

            return cube.gameObject;
        }
Пример #2
0
        /// <summary>
        /// Resolves the binding.
        /// </summary>
        /// <param name="binding">Binding to be resolved.</param>
        /// <param name="type">Binding type.</param>
        /// <param name="member">Member for which the binding is being resolved.</param>
        /// <param name="parentInstance">Parent object in which the resolve is occuring.</param>
        /// <param name="identifier">The binding identifier to be looked for.</param>
        /// <returns>The resolved instance from the binding.</returns>
        protected object ResolveBinding(BindingInfo binding,
                                        Type type,
                                        InjectionMember member,
                                        object parentInstance,
                                        object identifier)
        {
            //Condition evaluation.
            if (binding.condition != null)
            {
                var context = new InjectionContext()
                {
                    member         = member,
                    memberType     = type,
                    identifier     = identifier,
                    parentType     = (parentInstance != null ? parentInstance.GetType() : null),
                    parentInstance = parentInstance,
                    injectType     = binding.type
                };

                if (!binding.condition(context))
                {
                    return(null);
                }
            }

            //Identifier evaluation.
            bool resolveByIdentifier  = identifier != null;
            bool bindingHasIdentifier = binding.identifier != null;

            if ((!resolveByIdentifier && bindingHasIdentifier) ||
                (resolveByIdentifier && !bindingHasIdentifier) ||
                (resolveByIdentifier && bindingHasIdentifier && !binding.identifier.Equals(identifier)))
            {
                return(null);
            }

            //Instance evaluation.
            object instance = null;

            if (this.bindingEvaluation != null)
            {
                var delegates = this.bindingEvaluation.GetInvocationList();
                for (int delegateIndex = 0; delegateIndex < delegates.Length; delegateIndex++)
                {
                    instance = ((BindingEvaluationHandler)delegates[delegateIndex]).Invoke(this, ref binding);
                }
            }

            if (instance == null)
            {
                if (binding.instanceType == BindingInstance.Transient)
                {
                    instance = this.Instantiate(binding.value as Type);
                }
                else if (binding.instanceType == BindingInstance.Factory)
                {
                    var context = new InjectionContext()
                    {
                        member         = member,
                        memberType     = type,
                        identifier     = identifier,
                        parentType     = (parentInstance != null ? parentInstance.GetType() : null),
                        parentInstance = parentInstance,
                        injectType     = binding.type
                    };

                    instance = (binding.value as IFactory).Create(context);
                }
                else
                {
                    //Binding is a singleton object.

                    //If the binding value is a type, instantiate it.
                    if (binding.value is Type)
                    {
                        binding.value = this.Instantiate(binding.value as Type);
                    }

                    instance = binding.value;
                }
            }

            if (this.bindingResolution != null)
            {
                this.bindingResolution(this, ref binding, ref instance);
            }

            return(instance);
        }
Пример #3
0
        public object Create(InjectionContext context)
        {
            var obj = new MockIClassWithAttributes();
            obj.field1 =
                obj.field2 =
                    obj.field3 = "Created from a Factory";

            return obj;
        }
Пример #4
0
 public object Create (InjectionContext context)
 {
     Debug.Log ("faktoria");
     return GameStateStore.Instance;
 }