Exemple #1
0
		private bool CanResolvePendingDependencies(CreationContext context)
		{
			// detect circular dependencies
			if (context.HandlerIsCurrentlyBeingResolved(this))
				return false;

            foreach (Type service in DependenciesByService.Keys)
			{
				// a self-dependency is not allowed
				var handler = Kernel.GetHandler(service);
				if (handler == this)
					return false;

				// ask the kernel
				if (!Kernel.HasComponent(service))
					return false;
			}
			
			return DependenciesByKey.Count == 0;
		}
		private bool HasComponentInValidState(CreationContext context, Type service)
		{
			IHandler[] handlers = kernel.GetHandlers(service);

			foreach (IHandler handler in handlers)
			{
				if (context == null || context.HandlerIsCurrentlyBeingResolved(handler) == false)
					return IsHandlerInValidState(handler);
			}

			return false;
		}
		private IHandler TryGetHandlerFromKernel(DependencyModel dependency, CreationContext context)
		{
			// we are doing it in two stages because it is likely to be faster to a lookup
			// by key than a linear search
			IHandler handler = kernel.GetHandler(dependency.TargetType);
			if (context.HandlerIsCurrentlyBeingResolved(handler) == false)
				return handler;
			
			// make a best effort to find another one that fit

			IHandler[] handlers = kernel.GetHandlers(dependency.TargetType);

			foreach(IHandler maybeCorrectHandler in handlers)
			{
				if (context.HandlerIsCurrentlyBeingResolved(maybeCorrectHandler) == false)
				{
					handler = maybeCorrectHandler;
					break;
				}
			}
			return handler;
		}
		protected virtual object ResolveServiceDependency(CreationContext context, ComponentModel model,
														  DependencyModel dependency)
		{
			IHandler handler = null;

			if (dependency.DependencyType == DependencyType.Service)
			{
				ParameterModel parameter = ObtainParameterModelMatchingDependency(dependency, model);

				if (parameter != null)
				{
					// User wants to override, we then 
					// change the type to ServiceOverride

					dependency.DependencyKey = ExtractComponentKey(parameter.Value, parameter.Name);
					dependency.DependencyType = DependencyType.ServiceOverride;
				}
			}

			if (dependency.TargetType == typeof(IKernel))
			{
				return kernel;
			}
			else
			{
				if (dependency.DependencyType == DependencyType.ServiceOverride)
				{
					handler = kernel.GetHandler(dependency.DependencyKey);
				}
				else
				{
					IHandler[] handlers = kernel.GetHandlers(dependency.TargetType);
					foreach (IHandler maybeCorrectHandler in handlers)
					{
						if (context.HandlerIsCurrentlyBeingResolved(maybeCorrectHandler) == false)
						{
							handler = maybeCorrectHandler;
							break;
						}
					}

					if (handler == null)
					{
						throw new DependencyResolverException(
							"Cycle detected in configuration.\r\n" +
							"Component " + model.Name + " has a dependency on " +
							dependency.TargetType + ", but it doesn't provide an override.\r\n" +
							"You must provide an override if a component " +
							"has a dependency on a service that it - itself - provides");
					}

				}
			}

			if (handler == null) return null;

			context = RebuildContextForParameter(context, dependency.TargetType);

			return handler.Resolve(context);
		}