public override ISomeProvider Create(IInjectContext injectContext) { return(new SomeProvider() { ConsumerType = injectContext.DeclaringType }); }
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)); }
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); }
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); }
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); }
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); } }
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()); }
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)); } }
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); } }
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); } } }
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); }
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(); } }
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); }
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); }
public virtual object Resolve(Type type, object target, IInjectContext injectContext) { throw new NotImplementedException(); }
public abstract T Create(IInjectContext injectionContext);
object IFactory.Create(IInjectContext injectionContext) { return(Create(injectionContext)); }
public object Resolve(Type type, IInjectContext parentContext) { return(ResolveWithCategory(type, null, parentContext)); }
public void InjectBy <K>(IInjectContext <K> context) { context.InjectTo(this); }
public override ISomeTypeTest9 Create(IInjectContext injectContext) { NumCreated++; ItemInjectContext = injectContext; return(new SomeTypeTest9()); }
public InjectContext(IContainer container, Type declaringType, IInjectContext parentContext) { Container = container; DeclaringType = declaringType; ParentContext = parentContext; }
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); }