public void GettingComponentWithMissingDependencyFails() { IPicoContainer picoContainer = CreatePicoContainerWithDependsOnTouchableOnly(); try { picoContainer.GetComponentInstance(typeof(DependsOnTouchable)); Assert.Fail("should need a Touchable"); } catch (UnsatisfiableDependenciesException e) { Assert.AreSame( picoContainer.GetComponentAdapterOfType(typeof(DependsOnTouchable)).ComponentImplementation, e.UnsatisfiableComponentAdapter.ComponentImplementation); IList unsatisfiableDependencies = e.UnsatisfiableDependencies; Assert.AreEqual(1, unsatisfiableDependencies.Count); Assert.AreEqual(typeof(ITouchable), unsatisfiableDependencies[0]); } }
public IComponentAdapter GetComponentAdapterOfType(Type componentType) { IComponentAdapter adapterByKey = GetComponentAdapter(componentType); if (adapterByKey != null) { return(adapterByKey); } IList found = GetComponentAdaptersOfType(componentType); if (found.Count == 1) { return((IComponentAdapter)found[0]); } if (found.Count == 0) { if (parent != null) { return(parent.GetComponentAdapterOfType(componentType)); } else { return(null); } } else { Type[] foundClasses = new Type[found.Count]; for (int i = 0; i < foundClasses.Length; i++) { IComponentAdapter componentAdapter = (IComponentAdapter)found[i]; foundClasses[i] = componentAdapter.ComponentImplementation; } throw new AmbiguousComponentResolutionException(componentType, foundClasses); } }
private IComponentAdapter GetTargetAdapter(IPicoContainer container, Type expectedType, IComponentAdapter excludeAdapter) { if (componentKey != null) { // key tells us where to look so we follow return container.GetComponentAdapter(componentKey); } else if (excludeAdapter == null) { return container.GetComponentAdapterOfType(expectedType); } else { Object excludeKey = excludeAdapter.ComponentKey; IComponentAdapter byKey = container.GetComponentAdapter(expectedType); if (byKey != null) { if (byKey.ComponentKey.Equals(excludeKey)) { return null; } return byKey; } IList found = container.GetComponentAdaptersOfType(expectedType); IComponentAdapter exclude = null; foreach (IComponentAdapter work in found) { if (work.ComponentKey.Equals(excludeKey)) { exclude = work; } } found.Remove(exclude); if (found.Count == 0) { if (container.Parent != null) { return container.Parent.GetComponentAdapterOfType(expectedType); } else { return null; } } else if (found.Count == 1) { return (IComponentAdapter) found[0]; } else { Type[] foundTypes = new Type[found.Count]; for (int i = 0; i < foundTypes.Length; i++) { foundTypes[i] = ((IComponentAdapter) found[i]).ComponentImplementation; } throw new AmbiguousComponentResolutionException(expectedType, foundTypes); } } }
private IComponentAdapter GetTargetAdapter(IPicoContainer container, Type expectedType, IComponentAdapter excludeAdapter) { if (componentKey != null) { // key tells us where to look so we follow return(container.GetComponentAdapter(componentKey)); } else if (excludeAdapter == null) { return(container.GetComponentAdapterOfType(expectedType)); } else { Object excludeKey = excludeAdapter.ComponentKey; IComponentAdapter byKey = container.GetComponentAdapter(expectedType); if (byKey != null) { if (byKey.ComponentKey.Equals(excludeKey)) { return(null); } return(byKey); } IList found = container.GetComponentAdaptersOfType(expectedType); IComponentAdapter exclude = null; foreach (IComponentAdapter work in found) { if (work.ComponentKey.Equals(excludeKey)) { exclude = work; } } found.Remove(exclude); if (found.Count == 0) { if (container.Parent != null) { return(container.Parent.GetComponentAdapterOfType(expectedType)); } else { return(null); } } else if (found.Count == 1) { return((IComponentAdapter)found[0]); } else { Type[] foundTypes = new Type[found.Count]; for (int i = 0; i < foundTypes.Length; i++) { foundTypes[i] = ((IComponentAdapter)found[i]).ComponentImplementation; } throw new AmbiguousComponentResolutionException(expectedType, foundTypes); } } }