public void RES_failingVerificationWithCyclicDependencyException() { if ((GetComponentAdapterNature() & RESOLVING) > 0) { Hashtable cycleInstances = new Hashtable(); IObjectReference cycleCheck = new SimpleReference(); object[] wrapperDependencies = new Object[] { cycleInstances, cycleCheck }; IMutablePicoContainer picoContainer = new DefaultPicoContainer(CreateDefaultComponentAdapterFactory()); IComponentAdapter componentAdapter = prepRES_failingVerificationWithCyclicDependencyException(picoContainer); Assert.AreSame(GetComponentAdapterType(), componentAdapter.GetType()); Assert.IsTrue(picoContainer.ComponentAdapters.Contains(componentAdapter)); IPicoContainer wrappedPicoContainer = WrapComponentInstances(typeof(CycleDetectorComponentAdapter), picoContainer, wrapperDependencies); try { componentAdapter.Verify(wrappedPicoContainer); Assert.Fail("Thrown PicoVerificationException excpected"); } catch (CyclicDependencyException cycle) { object[] dependencies = cycle.Dependencies; Assert.AreSame(dependencies[0], dependencies[dependencies.Length - 1]); } } }
private ImplementationHidingCachingPicoContainer(IPicoContainer parent, CachingComponentAdapterFactory caf, ILifecycleManager lifecycleManager) : base(new ImplementationHidingPicoContainer(caf, parent, lifecycleManager)) { this.caf = caf; this.lifecycleManager = lifecycleManager; }
public override Object GetComponentInstance(IPicoContainer container) { Object componentKey = Delegate.ComponentKey; Type[] types; if (componentKey is Type && ((Type)Delegate.ComponentKey).IsInterface) { types = new Type[] { (Type)Delegate.ComponentKey }; } else if (componentKey is Type[]) { types = (Type[])componentKey; } else { if (strict) { throw new PicoIntrospectionException("In strict mode, " + GetType().Name + " only allows components registered with interface keys (System.Type or System.Type[])"); } return(Delegate.GetComponentInstance(container)); } return(CreateProxy(types, container)); }
public override object GetComponentInstance(IPicoContainer container) { Object result = base.GetComponentInstance(container); list.Add(result); return(result); }
/// <summary> /// Resolve the parameter for the expected type. The method will return <code>null</code> /// If the expected type is not one of the collection types {@link Array}, /// {@link Collection}or {@link Map}. An empty collection is only a valid resolution, if /// the <code>emptyCollection</code> flag was set. /// </summary> /// <param name="container"></param> /// <param name="adapter"></param> /// <param name="expectedType"></param> /// <returns>the instance of the collection type or <code>null</code></returns> public Object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { // type check is done in isResolvable Object result = null; Type collectionType = GetCollectionType(expectedType); if (collectionType != null) { IDictionary dictionary = GetMatchingComponentAdapters(container, adapter, componentKeyType, GetValueType(expectedType)); if (typeof(Array).IsAssignableFrom(collectionType)) { result = GetArrayInstance(container, expectedType, dictionary); } else if (typeof(IDictionary).IsAssignableFrom(collectionType)) { result = GetDictionaryInstance(container, expectedType, dictionary); } else if (typeof(ICollection).IsAssignableFrom(collectionType)) { result = GetCollectionInstance(container, expectedType, dictionary); } else { throw new PicoIntrospectionException(expectedType.Name + " is not a collective type"); } } return(result); }
/// <summary> /// Creates a new container with a parent container. /// </summary> /// <param name="caf"></param> /// <param name="parent"></param> /// <param name="lifecycleManager"></param> public CachingPicoContainer(CachingComponentAdapterFactory caf, IPicoContainer parent, ILifecycleManager lifecycleManager) : base(new DefaultPicoContainer(caf, parent, lifecycleManager)) { this.lifecycleManager = lifecycleManager; this.caf = caf; }
/// <summary> /// Verify a successful dependency resolution of the parameter for the expected type. The /// method will only return if the expected type is one of the collection types {@link Array}, /// {@link Collection}or {@link Map}. An empty collection is only a valid resolution, if /// the <code>emptyCollection</code> flag was set. /// </summary> /// <param name="container"></param> /// <param name="adapter"></param> /// <param name="expectedType"></param> public void Verify(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { Type collectionType = GetCollectionType(expectedType); if (collectionType != null) { Type valueType = GetValueType(expectedType); ICollection componentAdapters = GetMatchingComponentAdapters(container, adapter, componentKeyType, valueType).Values; if (componentAdapters.Count == 0) { if (!emptyCollection) { throw new PicoIntrospectionException(expectedType.FullName + " not resolvable, no components of type " + GetValueType(expectedType).FullName + " available"); } } else { foreach (IComponentAdapter componentAdapter in componentAdapters) { componentAdapter.Verify(container); } } } else { throw new PicoIntrospectionException(expectedType.Name + " is not a collective type"); } return; }
protected override IMutablePicoContainer CreatePicoContainer(IPicoContainer parent, ILifecycleManager lifecycleManager) { return new ImplementationHidingCachingPicoContainer( new CachingComponentAdapterFactory(new DefaultComponentAdapterFactory()), parent, lifecycleManager); }
/// <summary> /// Creates a new container with a parent container. /// </summary> /// <param name="caf"></param> /// <param name="parent"></param> /// <param name="lifecycleManager"></param> public ImplementationHidingPicoContainer(IComponentAdapterFactory caf, IPicoContainer parent, ILifecycleManager lifecycleManager) : base(new DefaultPicoContainer(caf, parent, lifecycleManager)) { this.caf = caf; this.lifecycleManager = lifecycleManager; }
/// <summary> /// Resolve the parameter for the expected type. The method will return <code>null</code> /// If the expected type is not one of the collection types {@link Array}, /// {@link Collection}or {@link Map}. An empty collection is only a valid resolution, if /// the <code>emptyCollection</code> flag was set. /// </summary> /// <param name="container"></param> /// <param name="adapter"></param> /// <param name="expectedType"></param> /// <returns>the instance of the collection type or <code>null</code></returns> public Object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { // type check is done in isResolvable Object result = null; Type collectionType = GetCollectionType(expectedType); if (collectionType != null) { IDictionary dictionary = GetMatchingComponentAdapters(container, adapter, componentKeyType, GetValueType(expectedType)); if (typeof (Array).IsAssignableFrom(collectionType)) { result = GetArrayInstance(container, expectedType, dictionary); } else if (typeof (IDictionary).IsAssignableFrom(collectionType)) { result = GetDictionaryInstance(container, expectedType, dictionary); } else if (typeof (ICollection).IsAssignableFrom(collectionType)) { result = GetCollectionInstance(container, expectedType, dictionary); } else { throw new PicoIntrospectionException(expectedType.Name + " is not a collective type"); } } return result; }
protected virtual IPicoContainer WrapComponentInstances(Type decoratingComponentAdapterClass, IPicoContainer picoContainer, object[] wrapperDependencies) { Assert.IsTrue(typeof (DecoratingComponentAdapter).IsAssignableFrom(decoratingComponentAdapterClass)); IMutablePicoContainer mutablePicoContainer = new DefaultPicoContainer(); int size = (wrapperDependencies != null ? wrapperDependencies.Length : 0) + 1; ICollection allComponentAdapters = picoContainer.ComponentAdapters; foreach (object adapter in allComponentAdapters) { IParameter[] parameters = new IParameter[size]; parameters[0] = new ConstantParameter(adapter); for (int i = 1; i < parameters.Length; i++) { parameters[i] = new ConstantParameter(wrapperDependencies[i - 1]); } IMutablePicoContainer instantiatingPicoContainer = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory()); instantiatingPicoContainer.RegisterComponentImplementation("decorator", decoratingComponentAdapterClass, parameters); mutablePicoContainer.RegisterComponent( (IComponentAdapter) instantiatingPicoContainer.GetComponentInstance("decorator")); } return mutablePicoContainer; }
public override Object GetComponentInstance(IPicoContainer container) { Object componentKey = Delegate.ComponentKey; Type[] types; if (componentKey is Type && ((Type) Delegate.ComponentKey).IsInterface) { types = new Type[] {(Type) Delegate.ComponentKey}; } else if (componentKey is Type[]) { types = (Type[]) componentKey; } else { if (strict) { throw new PicoIntrospectionException("In strict mode, " + GetType().Name + " only allows components registered with interface keys (System.Type or System.Type[])"); } return Delegate.GetComponentInstance(container); } return CreateProxy(types, container); }
protected override IMutablePicoContainer CreatePicoContainer(IPicoContainer parent, ILifecycleManager lifecycleManager) { return (new ImplementationHidingCachingPicoContainer( new CachingComponentAdapterFactory(new DefaultComponentAdapterFactory()), parent, lifecycleManager)); }
protected virtual IPicoContainer WrapComponentInstances(Type decoratingComponentAdapterClass, IPicoContainer picoContainer, object[] wrapperDependencies) { Assert.IsTrue(typeof(DecoratingComponentAdapter).IsAssignableFrom(decoratingComponentAdapterClass)); IMutablePicoContainer mutablePicoContainer = new DefaultPicoContainer(); int size = (wrapperDependencies != null ? wrapperDependencies.Length : 0) + 1; ICollection allComponentAdapters = picoContainer.ComponentAdapters; foreach (object adapter in allComponentAdapters) { IParameter[] parameters = new IParameter[size]; parameters[0] = new ConstantParameter(adapter); for (int i = 1; i < parameters.Length; i++) { parameters[i] = new ConstantParameter(wrapperDependencies[i - 1]); } IMutablePicoContainer instantiatingPicoContainer = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory()); instantiatingPicoContainer.RegisterComponentImplementation("decorator", decoratingComponentAdapterClass, parameters); mutablePicoContainer.RegisterComponent( (IComponentAdapter)instantiatingPicoContainer.GetComponentInstance("decorator")); } return(mutablePicoContainer); }
protected virtual IMutablePicoContainer CreateContainerFromScript(IPicoContainer parentContainer, IList assemblies) { IScript script = CreateScript(assemblies); script.Parent = parentContainer; return script.Compose(); }
protected override IMutablePicoContainer CreatePicoContainer(IPicoContainer parent) { return (new DefaultPicoContainer( new CachingComponentAdapterFactory( new ImplementationHidingComponentAdapterFactory( new ConstructorInjectionComponentAdapterFactory(), false)), parent)); }
public override object GetComponentInstance(IPicoContainer container) { if (constructorInjectionGuard == null) { constructorInjectionGuard = new ConstructorInjectionGuard(this, container); } return(constructorInjectionGuard.Observe(ComponentImplementation)); }
protected override IMutablePicoContainer CreatePicoContainer(IPicoContainer parent) { return new DefaultPicoContainer( new CachingComponentAdapterFactory( new ImplementationHidingComponentAdapterFactory( new ConstructorInjectionComponentAdapterFactory(), false)), parent); }
/// <summary> /// Gets the component instance. Only one instance is created of the type /// </summary> /// <returns>a component instance</returns> /// <exception cref="PicoContainer.PicoInitializationException">if the component could not be instantiated.</exception> /*public override object ComponentInstance * { * get * { * if (instanceReference.Get() == null) * { * instanceReference.Set(base.ComponentInstance); * } * return instanceReference.Get(); * } * * }*/ public override object GetComponentInstance(IPicoContainer container) { if (instanceReference.Get() == null) { instanceReference.Set(base.GetComponentInstance(container)); } return(instanceReference.Get()); }
public override object GetComponentInstance(IPicoContainer container) { if (constructorInjectionGuard == null) { constructorInjectionGuard = new ConstructorInjectionGuard(this, container); } return constructorInjectionGuard.Observe(ComponentImplementation); }
public override void Verify(IPicoContainer container) { if (verifyingGuard == null) { verifyingGuard = new VerifyingGuard(this, container); } verifyingGuard.Observe(ComponentImplementation); }
public override Object GetComponentInstance(IPicoContainer container) { if (setterInjectionGuard == null) { setterInjectionGuard = new SetterInjectionGuard(container, this); } return(setterInjectionGuard.Observe(ComponentImplementation)); }
/// <summary> /// Gets the component instance. Only one instance is created of the type /// </summary> /// <returns>a component instance</returns> /// <exception cref="PicoContainer.PicoInitializationException">if the component could not be instantiated.</exception> /*public override object ComponentInstance { get { if (instanceReference.Get() == null) { instanceReference.Set(base.ComponentInstance); } return instanceReference.Get(); } }*/ public override object GetComponentInstance(IPicoContainer container) { if (instanceReference.Get() == null) { instanceReference.Set(base.GetComponentInstance(container)); } return instanceReference.Get(); }
protected IPicoContainer BuildContainer(ScriptedContainerBuilder builder, IPicoContainer parentContainer, IList assemblies) { SimpleReference sr = new SimpleReference(); SimpleReference pr = new SimpleReference(); pr.Set(parentContainer); builder.BuildContainer(sr, pr, assemblies); return (IPicoContainer) sr.Get(); }
public virtual void Dispose(IPicoContainer node) { IList disposables = node.GetComponentInstancesOfType(typeof (IDisposable)); for (int i = disposables.Count - 1; 0 <= i; i--) { DoMethod(disposeMethod, disposables[i]); } }
public void Verify(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { if (!expectedType.IsInstanceOfType(constantValue)) { throw new PicoIntrospectionException(expectedType.FullName + " is not assignable from " + constantValue.GetType().FullName); } }
public virtual void Start(IPicoContainer node) { IList startables = node.GetComponentInstancesOfType(typeof (IStartable)); foreach (object startable in startables) { DoMethod(startMethod, startable); } }
public virtual void Stop(IPicoContainer node) { IList startables = node.GetComponentInstancesOfType(typeof (IStartable)); for (int i = startables.Count - 1; 0 <= i; i--) { DoMethod(stopMethod, startables[i]); } }
public virtual bool AddChildContainer(IPicoContainer child) { if (children.Contains(child)) { return(false); } children.Add(child, child); return(true); }
public virtual object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter componentAdapter = ResolveAdapter(container, adapter, expectedType); if (componentAdapter != null) { return container.GetComponentInstance(componentAdapter.ComponentKey); } return null; }
public virtual void Stop(IPicoContainer node) { IList startables = node.GetComponentInstancesOfType(typeof(IStartable)); for (int i = startables.Count - 1; 0 <= i; i--) { DoMethod(stopMethod, startables[i]); } }
public override void Verify(IPicoContainer container) { if (verifyingGuard == null) { verifyingGuard = new DefaultVerifyingGuard(this, container); } verifyingGuard.Observe(ComponentImplementation); }
public virtual void Start(IPicoContainer node) { IList startables = node.GetComponentInstancesOfType(typeof(IStartable)); foreach (object startable in startables) { DoMethod(startMethod, startable); } }
public virtual void Dispose(IPicoContainer node) { IList disposables = node.GetComponentInstancesOfType(typeof(IDisposable)); for (int i = disposables.Count - 1; 0 <= i; i--) { DoMethod(disposeMethod, disposables[i]); } }
protected void SetUp() { events = new StringBuilder(); one = new TestComponent("One", events); two = new TestComponent("Two", events); three = new TestComponent("Three", events); mock = new DynamicMock(typeof (IPicoContainer)); pico = (IPicoContainer) mock.MockInstance; }
public void RegisteredComponentsExistAndAreTheCorrectTypes() { IPicoContainer pico = CreatePicoContainerWithTouchableAndDependsOnTouchable(); Assert.IsNotNull(pico.GetComponentAdapter(typeof(ITouchable))); Assert.IsNotNull(pico.GetComponentAdapter(typeof(DependsOnTouchable))); Assert.IsTrue(pico.GetComponentInstance(typeof(ITouchable)) is ITouchable); Assert.IsTrue(pico.GetComponentInstance(typeof(DependsOnTouchable)) is DependsOnTouchable); Assert.IsNull(pico.GetComponentAdapter(typeof(ICollection))); }
public virtual object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter componentAdapter = ResolveAdapter(container, adapter, expectedType); if (componentAdapter != null) { return(container.GetComponentInstance(componentAdapter.ComponentKey)); } return(null); }
public virtual void Verify(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter componentAdapter = ResolveAdapter(container, adapter, expectedType); if (componentAdapter == null) { throw new PicoIntrospectionException(expectedType.Name + " is not resolvable"); } componentAdapter.Verify(container); }
public virtual bool RemoveChildContainer(IPicoContainer child) { if (children.Contains(child)) { children.Remove(child); return(true); } return(false); }
/// <summary> /// Check for a successful dependency resolution of the parameter for the expected type. The /// dependency can only be satisfied if the expected type is one of the collection types /// {@link Array},{@link Collection}or {@link Map}. An empty collection is only a valid /// resolution, if the <code>emptyCollection</code> flag was set. /// </summary> /// <param name="container"></param> /// <param name="adapter"></param> /// <param name="expectedType"></param> /// <returns><code>true</code> if matching components were found or an empty collective type is allowed</returns> public bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { Type collectionType = GetCollectionType(expectedType); Type valueType = GetValueType(expectedType); return (collectionType != null && (emptyCollection || GetMatchingComponentAdapters(container, adapter, componentKeyType, valueType).Count > 0)); }
public override object ResolveInstance(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { // type check is done in isResolvable Object result = base.ResolveInstance(container, adapter, expectedType); if (result == null && collectionParameter != null) { result = collectionParameter.ResolveInstance(container, adapter, expectedType); } return(result); }
private IComponentAdapter ResolveAdapter(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter result = GetTargetAdapter(container, expectedType, adapter); if (result != null && !expectedType.IsAssignableFrom(result.ComponentImplementation)) { return(null); } return(result); }
/// <summary> /// Creates a new container with a custom ComponentAdapterFactory and a parent container. /// Important note about caching: If you intend the components to be cached, you should pass /// in a factory that creates CachingComponentAdapter instances, such as for example /// other ComponentAdapterFactories. /// </summary> /// <param name="componentAdapterFactory">the factory to use for creation of ComponentAdapters.</param> /// <param name="parent">the parent container (used for component dependency lookups).</param> /// <param name="lifecycleManager">the lifecycle manager to manage start/stop/dispose calls on the container.</param> public DefaultPicoContainer(IComponentAdapterFactory componentAdapterFactory, IPicoContainer parent, ILifecycleManager lifecycleManager) { this.lifecycleManager = lifecycleManager; if (componentAdapterFactory == null) { throw new NullReferenceException("componentAdapterFactory"); } this.componentAdapterFactory = componentAdapterFactory; this.parent = parent; // == null ? null : new ImmutablePicoContainer(parent); }
private IComponentAdapter ResolveAdapter(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { IComponentAdapter result = GetTargetAdapter(container, expectedType, adapter); if (result != null && !expectedType.IsAssignableFrom(result.ComponentImplementation)) { return null; } return result; }
public Object getComponentInstance(IPicoContainer container) { if (set.Contains(this)) { reference.Set(this); } else { set.Add(this, this); } return(base.GetComponentInstance(container)); }
public virtual bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { try { Verify(container, adapter, expectedType); return(true); } catch (PicoIntrospectionException) { return(false); } }
public virtual bool IsResolvable(IPicoContainer container, IComponentAdapter adapter, Type expectedType) { try { Verify(container, adapter, expectedType); return true; } catch (PicoIntrospectionException) { return false; } }
private Object CreateProxy(Type[] interfaces, IPicoContainer container) { try { ProxyGenerator proxyGenerator = new ProxyGenerator(); object componentInstance = Delegate.GetComponentInstance(container); return proxyGenerator.CreateProxy(interfaces, new PicoInterceptor(), componentInstance); } catch (ArgumentException e) { throw new PicoIntrospectionException("Error creating a dynamic proxy for implementation hiding", e); } }
private IParameter[] GetMatchingParameterListForSetters(IPicoContainer container) { if (setters == null) { InitializeSetterAndTypeLists(); } IParameter[] matchingParameterList = new IParameter[setters.Count]; ArrayList nonMatchingParameterPositions = new ArrayList(); // was Set IParameter[] currentParameters = parameters != null ? parameters : CreateDefaultParameters(setterTypes); for (int i = 0; i < currentParameters.Length; i++) { IParameter parameter = currentParameters[i]; bool failedDependency = true; for (int j = 0; j < setterTypes.Length; j++) { if (matchingParameterList[j] == null && parameter.IsResolvable(container, this, setterTypes[j])) { matchingParameterList[j] = parameter; failedDependency = false; break; } } if (failedDependency) { nonMatchingParameterPositions.Add(i); } } ArrayList unsatisfiableDependencyTypes = new ArrayList(); for (int i = 0; i < matchingParameterList.Length; i++) { if (matchingParameterList[i] == null) { unsatisfiableDependencyTypes.Add(setterTypes[i]); } } if (unsatisfiableDependencyTypes.Count > 0) { throw new UnsatisfiableDependenciesException(this, unsatisfiableDependencyTypes); } else if (nonMatchingParameterPositions.Count > 0) { throw new PicoInitializationException("Following parameters do not match any of the setters for " + ComponentImplementation + ": " + nonMatchingParameterPositions.ToString()); } return matchingParameterList; }
protected override IMutablePicoContainer CreateContainer(IPicoContainer parentContainer, object assemblyScope) { try { return CreateContainerFromScript(parentContainer, assemblyScope); } finally { try { stream.Close(); } catch (IOException) { } } }
protected override IMutablePicoContainer CreateContainer(IPicoContainer parentContainer, IList assemblies) { try { return CreateContainerFromScript(parentContainer, assemblies); } finally { try { StreamReader.Close(); } catch (IOException) { // ignore } } }
protected override IMutablePicoContainer CreateContainerFromScript(IPicoContainer parentContainer, object assemblyScope) { Type type = GetCompiledType(this.stream, (IList) assemblyScope); object creator = Activator.CreateInstance(type); if (parentContainer != null) { PropertyInfo info = GetParentProperty(type); if (info == null) { throw new PicoCompositionException("A parent container is provided but the composition script has no property defined for accepting the parent.\nPlease specify a property called Parent and it should be of the type PicoContainer"); } info.SetValue(creator, parentContainer, new object[] {}); } MethodInfo mi = GetComposeMethod(type); return (IMutablePicoContainer) mi.Invoke(creator, new object[] {}); }
public DefaultPicoContainer(IPicoContainer parent) : this(new DefaultComponentAdapterFactory(), parent) { }
public DefaultPicoContainer(IComponentAdapterFactory componentAdapterFactory, IPicoContainer parent) : this(componentAdapterFactory, parent, new DefaultLifecycleManager()) { }