Beispiel #1
0
 public override ISomeProvider Create(IInjectContext injectContext)
 {
     return(new SomeProvider()
     {
         ConsumerType = injectContext.DeclaringType
     });
 }
Beispiel #2
0
        public override object Resolve(Type type, object target, IInjectContext injectContext)
        {
            GameObject gameObject = (GameObject)target;

            if (type.IsInterface)
            {
                LogHandler?.Invoke("Injector: Trying to resolve an interface instead of a concrete type! : {0} ", type);

                return(null);
            }

            _currentlyResolvingTypes.Add(type);

            // Ensure cached reflection...
            PreInjectReflection(type);

            if (_cachedMethodConstructor.ContainsKey(type) && _cachedMethodConstructor[type] != null)
            {
                // Include additional dependencies: GameObject target, Container...
                object instance = ExecuteStaticMethodConstructor(_container, injectContext, _cachedMethodConstructor[type], type, target, _container);

                _currentlyResolvingTypes.Remove(type);

                return(instance);
            }

            _currentlyResolvingTypes.Remove(type);

            return(gameObject.AddComponent(type));
        }
Beispiel #3
0
        public object Resolve(IBinding binding, IInjectContext injectContext, object target = null)
        {
            if (typeof(Component).IsAssignableFrom(binding.BoundType))
            {
                return(InternalResolveComponent(binding, injectContext, (GameObject)target));
            }

            // Default binding resolve will handle....
            return(null);
        }
Beispiel #4
0
        public object ResolveWithCategory(Type type, object category, IInjectContext parentContext)
        {
            // Check self-bindings...
            if (_bindings.ContainsKey(type))
            {
                IBinding binding = _bindings[type].GetBindingWithCategory(category);

                if (binding != null)
                {
                    return(binding.Resolve(parentContext));
                }
                else
                {
                    throw new MissingBindingException(type, category);
                }
            }

            // Cloned self-bindings...
            if (_clonedBindings.ContainsKey(type))
            {
                IBinding binding = _clonedBindings[type].GetBindingWithCategory(category);

                if (binding != null)
                {
                    return(binding.Resolve(parentContext));
                }
                else
                {
                    throw new MissingBindingException(type, category);
                }
            }

            // Check inherited bindings...
            if (_inheritedBindings.ContainsKey(type))
            {
                IBinding binding = _inheritedBindings[type].GetBindingWithCategory(category);

                if (binding != null)
                {
                    return(binding.Resolve(parentContext));
                }
                else
                {
                    throw new MissingBindingException(type, category);
                }
            }


            throw new MissingBindingException(type, category);
        }
Beispiel #5
0
        public object ResolveWithMiddleware(IInjectContext injectContext, object target = null)
        {
            foreach (var middleware in _bindingMiddleware)
            {
                var result = middleware.Resolve(this, injectContext, target);

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

            return(null);
        }
Beispiel #6
0
        public object Resolve(Type type, IInjectContext injectContext)
        {
            if (type.IsInterface)
            {
                LogHandler.Invoke("Injector: {0}", "Trying to resolve an interface instead of a concrete type! : " + type);

                return(null);
            }

            _currentlyResolvingTypes.Add(type);

            // Ensure cached reflection...
            PreInjectReflection(type);

            // Execute static constructor method
            if (_cachedMethodConstructor.ContainsKey(type) && _cachedMethodConstructor[type] != null)
            {
                // Include additional dependency of the container...
                object instance = ExecuteStaticMethodConstructor(_container, injectContext, _cachedMethodConstructor[type], type, _container);

                _currentlyResolvingTypes.Remove(type);

                return(instance);
            }

            // Execute constructor
            if (_cachedConstructor.ContainsKey(type) && _cachedConstructor[type] != null)
            {
                // Include additional dependency of the container...
                object instance = ExecuteConstructor(_container, injectContext, _cachedConstructor[type], type, _container);

                _currentlyResolvingTypes.Remove(type);

                return(instance);
            }
            else
            {
                _currentlyResolvingTypes.Remove(type);

                // Invoke defaut constructor...
                object instance = CreateInstance(type);

                return(instance);
            }
        }
Beispiel #7
0
        private void InjectMethod(IContainer container, IInjectContext parentContext, Type type, object obj, MethodInfoCache method)
        {
            ParameterInfoCache[] parameters = method.Parameters;
            if (parameters == null || parameters.Length == 0)
            {
                method.MethodInfo.Invoke(obj, null);
                return;
            }

            object[] paramObjects = new object[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                ParameterInfoCache parameter     = parameters[i];
                ParameterInfo      parameterInfo = parameter.ParameterInfo;

                // Can we resolve this?
                if (container.HasBindingFor(parameterInfo.ParameterType))
                {
                    // Prevent recursive / circular dependency...
                    if (parameterInfo.ParameterType.IsAssignableFrom(type) || _currentlyResolvingTypes.Contains(parameterInfo.ParameterType))
                    {
                        LogHandler?.Invoke("Injector: Circular dependency detected in Method {0}.{1}, parameter index: {2}", type, method.MethodInfo.Name, i);
                        paramObjects[i] = null;
                    }
                    else
                    {
                        InjectContext injectContext = new InjectContext(container, method.DeclaringType, parentContext);

                        Type parameterType = parameterInfo.ParameterType;

                        object paramInstance = container.ResolveWithCategory(parameterType, parameter.InjectAttribute?.Category, injectContext);
                        paramObjects[i] = paramInstance;
                    }
                }
                else
                {
                    LogHandler?.Invoke("Injector: {0} - Type is not bound in the container, assigning as null {1}, method {2}, parameter index: {2}", type, parameterInfo.ParameterType, method.MethodInfo.Name, i);
                    paramObjects[i] = null;
                }
            }

            method.MethodInfo.Invoke(obj, paramObjects.ToArray());
        }
Beispiel #8
0
        public object Resolve(IInjectContext parentContext, object target = null)
        {
            if (_inheritedFromBinding != null)
            {
                return(_inheritedFromBinding.Resolve(parentContext, target));
            }

            // Attempt to resolve with middleware first...
            var result = ResolveWithMiddleware(parentContext, target);

            if (result != null)
            {
                return(result);
            }
            else
            {
                return(InternalResolve(parentContext));
            }
        }
Beispiel #9
0
        public void InjectTo <K>(IInjectContext <K> context, object target)
        {
            if (injectMemberInfos == null)
            {
                ReflectMemebers();
            }

            foreach (var mInfo in injectMemberInfos)
            {
                if (!context.TryToGet((K)mInfo.Key, out var value))
                {
                    if (!mInfo.IsOptional)
                    {
                        throw new InjectValueNotFoundException(mInfo.Key);
                    }
                    continue;
                }
                mInfo.SetValue(target, value);
            }
        }
Beispiel #10
0
        public void ExtractFrom <K>(IInjectContext <K> context, object target)
        {
            if (extractMemberInfos == null)
            {
                ReflectMemebers();
            }

            foreach (var mInfo in extractMemberInfos)
            {
                var value = mInfo.GetValue(target);
                if (!mInfo.IsOptional)
                {
                    context.AddOrUpdate((K)mInfo.Key, value);
                }
                else if (value != null)
                {
                    context.AddOrUpdate((K)mInfo.Key, value);
                }
            }
        }
Beispiel #11
0
        private object InternalResolve(IInjectContext injectContext)
        {
            object instance;

            BoundObjects = BoundObjects ?? new List <object>();

            switch (BindingType)
            {
            case BindingType.Singleton:
                if (BoundObjects.Count == 0)
                {
                    instance = ResolveNew(injectContext);
                    BoundObjects.Add(instance);

                    Container.Injector.Inject(instance, injectContext);

                    Resolved?.Invoke(true, this, null);
                }
                else
                {
                    instance = BoundObjects.FirstOrDefault();

                    Resolved?.Invoke(false, this, null);
                }
                break;

            case BindingType.Transient:
                instance = ResolveNew(injectContext);
                Container.Injector.Inject(instance, injectContext);

                Resolved?.Invoke(true, this, null);
                break;

            default:
                throw new InvalidOperationException();
            }

            return(instance);
        }
Beispiel #12
0
        private object InternalResolveComponent(IBinding binding, IInjectContext injectContext, GameObject gameObject)
        {
            object addedComponent;

            binding.BoundObjects = binding.BoundObjects ?? new List <object>();

            switch (binding.BindingType)
            {
            case BindingType.Singleton:
                if (binding.BoundObjects.Count != 0)
                {
                    binding.NotifyResolved(false, gameObject);

                    return(binding.BoundObjects.FirstOrDefault());
                }

                gameObject     = (gameObject) ?? new GameObject(binding.BoundType.Name);
                addedComponent = binding.Container.Injector.Resolve(binding.BoundType, gameObject, injectContext);
                binding.Container.Injector.Inject(addedComponent, injectContext);
                binding.BoundObjects.Add(addedComponent);

                binding.NotifyResolved(true, gameObject);

                return(binding.BoundObjects.FirstOrDefault());

            case BindingType.Transient:
                gameObject     = (gameObject) ?? new GameObject(binding.BoundType.Name);
                addedComponent = binding.Container.Injector.Resolve(binding.BoundType, gameObject, injectContext);
                binding.Container.Injector.Inject(addedComponent, injectContext);

                binding.NotifyResolved(true, gameObject);

                return(addedComponent);

            default:
                throw new InvalidOperationException();
            }
        }
Beispiel #13
0
        private object ResolveNew(IInjectContext injectContext)
        {
            object instance;

            if (FactoryMethod != null)
            {
                instance = FactoryMethod?.Invoke(injectContext);
            }
            else if (FactoryType != null)
            {
                if (_cachedFactory == null)
                {
                    _cachedFactory = (IFactory)Container.Resolve(FactoryType);
                }

                instance = _cachedFactory.Create(injectContext);
            }
            else
            {
                instance = Container.Injector.Resolve(BoundType, injectContext);
            }

            return(instance);
        }
 public void ExtractTo <K>(IInjectContext <K> context)
 {
     context.ExtractFrom(this);
 }
Beispiel #15
0
        public void Inject(object objValue, IInjectContext parentContext)
        {
            Type tType = objValue.GetType();

            // Ensure cached reflection...
            PreInjectReflection(tType);

            _currentlyResolvingTypes.Add(tType);

            // Inject methods...
            for (int i = 0; i < _cachedMethods[tType].Count(); i++)
            {
                MethodInfoCache method = _cachedMethods[tType].ElementAt(i);

                InjectMethod(_container, parentContext, tType, objValue, method);
            }

            // Inject into Fields...
            for (int i = 0; i < _cachedFields[tType].Count(); i++)
            {
                MemberInfoCache memberCache = _cachedFields[tType].ElementAt(i);
                FieldInfo       field       = (FieldInfo)memberCache.MemberInfo;

                bool isCircularReference = false;

                if (field.FieldType.IsAssignableFrom(typeof(IContainer)))
                {
                    field.SetValue(objValue, _container);
                    continue;
                }

                foreach (Type type in _currentlyResolvingTypes)
                {
                    if (field.FieldType.IsAssignableFrom(type))
                    {
                        var binding = _container.GetBinding(field.FieldType, memberCache.InjectAttribute.Category);

                        if (binding != null && binding.BindingType == BindingType.Transient)
                        {
                            isCircularReference = true;
                        }
                    }
                }

                if (isCircularReference)
                {
                    LogHandler?.Invoke("Injector: Circular dependency detected in Field {0}.{1}", tType, field.Name);
                }
                else
                {
                    InjectContext injectContext = new InjectContext(_container, memberCache.DeclaringType, parentContext);

                    object fieldValue = _container.ResolveWithCategory(field.FieldType, memberCache.InjectAttribute.Category, injectContext);

                    field.SetValue(objValue, fieldValue);
                }
            }

            // Inject into Properties...
            for (int i = 0; i < _cachedProperties[tType].Count(); i++)
            {
                MemberInfoCache memberCache = _cachedProperties[tType].ElementAt(i);
                PropertyInfo    property    = (PropertyInfo)memberCache.MemberInfo;

                bool isCircularReference = false;

                if (property.PropertyType.IsAssignableFrom(typeof(IContainer)))
                {
                    property.SetValue(objValue, _container, null);
                    continue;
                }

                foreach (Type type in _currentlyResolvingTypes)
                {
                    if (property.PropertyType.IsAssignableFrom(type))
                    {
                        var binding = _container.GetBinding(property.PropertyType, memberCache.InjectAttribute.Category);

                        if (binding != null && binding.BindingType == BindingType.Transient)
                        {
                            isCircularReference = true;
                        }
                    }
                }

                if (isCircularReference)
                {
                    LogHandler?.Invoke("Injector: Circular dependency detected in Property {0}.{1}", tType, property.Name);
                }
                else
                {
                    InjectContext injectContext = new InjectContext(_container, memberCache.DeclaringType, parentContext);

                    object propertyValue = _container.ResolveWithCategory(property.PropertyType, memberCache.InjectAttribute.Category, injectContext);

                    property.SetValue(objValue, propertyValue, null);
                }
            }

            _currentlyResolvingTypes.Remove(tType);
        }
Beispiel #16
0
 public virtual object Resolve(Type type, object target, IInjectContext injectContext)
 {
     throw new NotImplementedException();
 }
Beispiel #17
0
 public abstract T Create(IInjectContext injectionContext);
Beispiel #18
0
 object IFactory.Create(IInjectContext injectionContext)
 {
     return(Create(injectionContext));
 }
Beispiel #19
0
 public object Resolve(Type type, IInjectContext parentContext)
 {
     return(ResolveWithCategory(type, null, parentContext));
 }
 public void InjectBy <K>(IInjectContext <K> context)
 {
     context.InjectTo(this);
 }
Beispiel #21
0
 public override ISomeTypeTest9 Create(IInjectContext injectContext)
 {
     NumCreated++;
     ItemInjectContext = injectContext;
     return(new SomeTypeTest9());
 }
Beispiel #22
0
 public InjectContext(IContainer container, Type declaringType, IInjectContext parentContext)
 {
     Container     = container;
     DeclaringType = declaringType;
     ParentContext = parentContext;
 }
Beispiel #23
0
        private object ExecuteConstructor(IContainer container, IInjectContext parentContext, ConstructorInfoCache constructor, Type type, params object[] addDependencies)
        {
            ParameterInfoCache[] parameters = constructor.Parameters;

            if (parameters == null || parameters.Length == 0)
            {
                return(constructor.ConstructorInfo.Invoke(null));
            }

            // Resolve parameters

            object[] paramObjects = new object[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                ParameterInfoCache parameter     = parameters[i];
                ParameterInfo      parameterInfo = parameter.ParameterInfo;

                // Can we resolve this?
                if (container.HasBindingFor(parameterInfo.ParameterType))
                {
                    bool isCircularDependency = false;

                    foreach (Type resolvingType in _currentlyResolvingTypes)
                    {
                        if (parameterInfo.ParameterType.IsAssignableFrom(resolvingType))
                        {
                            isCircularDependency = true;
                        }
                    }

                    // Prevent recursive / circular dependency...
                    if (isCircularDependency)
                    {
                        LogHandler?.Invoke("Injector: Circular dependency detected in Type {0} parameter index: {1}", type, i);
                        paramObjects[i] = null;
                    }
                    else
                    {
                        InjectContext injectContext = new InjectContext(container, constructor.DeclaringType, parentContext);

                        object paramInstance = container.ResolveWithCategory(parameterInfo.ParameterType, parameters[i].InjectAttribute?.Category, injectContext);
                        paramObjects[i] = paramInstance;
                    }
                }
                else
                {
                    bool hasAdditionalDependency = false;

                    // Check additional dependencies...
                    foreach (object dependency in addDependencies)
                    {
                        if (parameterInfo.ParameterType.IsInstanceOfType(dependency))
                        {
                            hasAdditionalDependency = true;
                            paramObjects[i]         = dependency;

                            break;
                        }
                    }

                    if (!hasAdditionalDependency)
                    {
                        LogHandler?.Invoke("Injector: {0} - Type is not bound in the container, assigning as null {1}, parameter index: {2}", type, parameterInfo.ParameterType, i);
                        paramObjects[i] = null;
                    }
                }
            }


            // Invoke constructor...
            object instance = constructor.ConstructorInfo.Invoke(paramObjects);

            return(instance);
        }