private void RegisterBindingProvider(InjectionKey injectionKey, IProvider provider) { if (!providers.ContainsKey(injectionKey)) { providers.Add(injectionKey, provider); } }
private void FetchConstructors() { injectableConstructors.Clear(); var constructors = referencedType.GetConstructors(INJECTABLE_BINDING_FLAGS); foreach (var constructorInfo in constructors) { var parameterInfos = constructorInfo.GetParameters(); if (parameterInfos.All(pi => pi.IsDefined(injectAttributeType, true))) { var argsKeys = new InjectionKey[parameterInfos.Length]; for (var paramIndex = 0; paramIndex < parameterInfos.Length; paramIndex++) { var parameterInfo = parameterInfos[paramIndex]; var customId = parameterInfo.GetCustomAttribute <InjectAttribute>(true).CustomId; var argKey = new InjectionKey(parameterInfo.ParameterType, customId); dependsOn.Add(argKey); argsKeys[paramIndex] = argKey; } var injectableConstructor = new InjectableConstructor(constructorInfo, argsKeys); injectableConstructors.Add(injectableConstructor); } } injectableConstructors.Sort((c1, c2) => c2.ParamKeys.Length.CompareTo(c1.ParamKeys.Length)); }
private void TryRegisterProvider(Injectable injectable) { if (injectable.Type != null && injectable.Type.IsSubclassOf(typeof(Provider)) && injectable.Tag == null) { var key = new InjectionKey(injectable.Type, null); var provider = ProviderFactory.CreateGeneric(injectable.Type); providers.Add(key, provider); } }
public IProvider GetProviderForDependency(Injectable injectable) { var key = new InjectionKey(injectable.Type, injectable.Tag); if (providers.ContainsKey(key)) { return(providers[key]); } return(null); }
private void FetchFields() { injectableFields.Clear(); var fields = referencedType.GetFields(INJECTABLE_BINDING_FLAGS) .Where(fi => fi.IsDefined(injectAttributeType, true)); foreach (var fieldInfo in fields) { var customId = fieldInfo.GetCustomAttribute <InjectAttribute>(true).CustomId; var fieldInjectionKey = new InjectionKey(fieldInfo.FieldType, customId); dependsOn.Add(fieldInjectionKey); injectableFields.Add(new InjectableField(fieldInfo, fieldInjectionKey)); } }
public void TestDependencyTreeOrdering() { var interface1Key = new InjectionKey(typeof(IInterface1)); var impl1Key = new InjectionKey(typeof(Impl1)); var interface2Key = new InjectionKey(typeof(IInterface2)); var impl2Key = new InjectionKey(typeof(Impl2)); var interface3Key = new InjectionKey(typeof(IInterface3)); //First, we'll mock a simple interface binding of interface1 to impl1 dependencyTree.AddDependency(interface1Key, impl1Key); var sorted = dependencyTree.Sorted().ToList(); Assert.AreEqual(sorted[0], interface3Key, "Interface 3 should be the first thing to resolve, since impl1 depends on it and interface1 depends on impl1"); Assert.AreEqual(sorted[1], interface2Key, "Interface 2 should be the second thing to resolve, since impl1 depends on it and interface1 depends on impl1"); Assert.AreEqual(sorted[2], impl1Key, "Impl 1 should be the third, since interface 1 depends on it"); Assert.AreEqual(sorted[3], interface1Key, "Finally, interface 1 should be resolved"); Assert.AreEqual(1, dependencyTree.RootKeys.Count(), "Should be a single root, interface1"); Assert.AreEqual(interface1Key, dependencyTree.RootKeys.First()); //Next, we'll bind interface 2 dependencyTree.AddDependency(interface2Key, impl2Key); sorted = dependencyTree.Sorted().ToList(); Assert.AreEqual(2, dependencyTree.RootKeys.Count(), "Should now be two roots"); Assert.IsTrue(dependencyTree.RootKeys.Contains(interface1Key)); Assert.IsTrue(dependencyTree.RootKeys.Contains(interface2Key)); Assert.AreEqual(sorted[0], impl2Key, "Now we should be resolving impl2 first"); Assert.AreEqual(sorted[1], interface2Key, "Interface 2 should be the second thing to resolve, since impl1 depends on it and interface1 depends on impl1"); Assert.AreEqual(sorted[2], interface3Key, "Interface 3 should be the third thing to resolve, since impl1 depends on it and interface1 depends on impl1"); Assert.AreEqual(sorted[3], impl1Key, "Impl 1 should be the fourth, since interface 1 depends on it"); Assert.AreEqual(sorted[4], interface1Key, "Finally, interface 1 should be resolved"); }
internal DuplicateBindingException(InjectionKey injectionKey) : base($"Dependency already bound for type \"{injectionKey.InjectedResourceType}\" with custom ID: \"{injectionKey.InjectedResourceName}\"") { }
public InjectableField(FieldInfo fieldInfo, InjectionKey injectionKey) { FieldInfo = fieldInfo; InjectionKey = injectionKey; }