public object Resolve(
			 CreationContext context,
			 ISubDependencyResolver parentResolver,
			 ComponentModel model,
			 DependencyModel dependency)
        {
            MockingStrategy strategy = _relatedRepository.GetStrategyFor(dependency);

            if (strategy.Instance != null)
                return strategy.Instance;
            if (strategy.Mock == MockingStrategyType.Mock)
            {
                //if a dependencywas already registered in the main controller, go and use it
                var registration = this._relatedRepository.Kernel.GetHandler(dependency.TargetType);
                object resolvedDependencyObject;
                if (registration == null)
                {
                    resolvedDependencyObject = MockRepository.GenerateStub(dependency.TargetType);
                }
                else
                {
                    resolvedDependencyObject = _relatedRepository.Resolve(dependency.TargetType);
                }
                _relatedRepository.OnMockCreated(resolvedDependencyObject, dependency.DependencyKey);
                return resolvedDependencyObject;
            }
            if (strategy.Mock == MockingStrategyType.Resolve)
                return _relatedRepository.Resolve(dependency.TargetType);

            return null;
        }
예제 #2
0
 public object Resolve(CreationContext context,
     ISubDependencyResolver parentResolver,
     ComponentModel model,
     DependencyModel dependency)
 {
     return _kernel.ResolveAll(dependency.TargetType.GetElementType(), null);
 }
예제 #3
0
 public object Resolve(CreationContext context, ISubDependencyResolver parentResolver,
     ComponentModel model,
     DependencyModel dependency)
 {
     Type t = dependency.TargetType.GetGenericArguments()[0];
     return kernel.ResolveAll(t, null);
 }
예제 #4
0
		public void Inspect(IHandler handler, DependencyModel[] missingDependencies, IKernel kernel)
		{
			if (handlersChecked.Add(handler) == false)
			{
				return;
			}
			Debug.Assert(missingDependencies.Length > 0, "missingDependencies.Length > 0");
			var uniqueOverrides = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
			message.AppendLine();
			message.AppendFormat("'{0}' is waiting for the following dependencies:", handler.ComponentModel.Name);
			message.AppendLine();
			foreach (var dependency in missingDependencies)
			{
				if (dependency.ReferencedComponentName != null)
				{
					// NOTE: that's a workaround for us having dependency twice potentially, once from configuration and once from actual type scan
					if (uniqueOverrides.Add(dependency.ReferencedComponentName))
					{
						InspectServiceOverrideDependency(dependency, kernel);
					}
				}
				else if (dependency.IsPrimitiveTypeDependency)
				{
					//hardcoding string as it's a most common type that is not value type but you wouldn't want to kave that as a service.
					InspectParameterDependency(dependency);
				}
				else
				{
					InspectServiceDependency(handler, dependency, kernel);
				}
			}
		}
예제 #5
0
 public object Resolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model,
     DependencyModel dependency)
 {
     if (dependency.TargetType == typeof (ISession))
         return SessionFactory.OpenSession();
     return SessionFactory.OpenStatelessSession();
 }
		/// <summary>
		///   Returns true if the resolver is able to satisfy the specified dependency.
		/// </summary>
		/// <param name = "context">Creation context, which is a resolver itself</param>
		/// <param name = "contextHandlerResolver">Parent resolver</param>
		/// <param name = "model">Model of the component that is requesting the dependency</param>
		/// <param name = "dependency">The dependency model</param>
		/// <returns>
		///   <c>true</c>
		///   if the dependency can be satisfied</returns>
		public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
		{
			// 1 - check for the dependency on CreationContext, if present
			if (CanResolveFromContext(context, contextHandlerResolver, model, dependency))
			{
				return true;
			}

			// 2 - check with the model's handler, if not the same as the parent resolver
			if (CanResolveFromHandler(context, contextHandlerResolver, model, dependency))
			{
				return true;
			}

			// 3 - check within parent resolver, if present
			if (CanResolveFromContextHandlerResolver(context, contextHandlerResolver, model, dependency))
			{
				return true;
			}

			// 4 - check within subresolvers
			if (CanResolveFromSubResolvers(context, contextHandlerResolver, model, dependency))
			{
				return true;
			}

			// 5 - normal flow, checking against the kernel
			return CanResolveFromKernel(context, model, dependency);
		}
예제 #7
0
		private DependencyModelExtended TrackDependency(ComponentModel model, MemberInfo info, DependencyModel dependencyModel)
		{
			DependencyModelExtended trackingKey = new DependencyModelExtended(model, dependencyModel, info);

			if (dependencies.Contains(trackingKey))
			{
				StringBuilder sb = new StringBuilder("A cycle was detected when trying to resolve a dependency. ");

				sb.Append("The dependency graph that resulted in a cycle is:");

				foreach(DependencyModel key in dependencies)
				{
					DependencyModelExtended extendedInfo = key as DependencyModelExtended;

					if (extendedInfo != null)
					{
						sb.AppendFormat("\r\n - {0} for {1} in type {2}",
						                key, extendedInfo.Info, extendedInfo.Info.DeclaringType);
					}
					else
					{
						sb.AppendFormat("\r\n - {0}", key);
					}
				}

				sb.AppendFormat("\r\n + {0} for {1} in {2}\r\n",
				                dependencyModel, info, info.DeclaringType);

				throw new CircularDependencyException(sb.ToString());
			}

			dependencies.Add(trackingKey);

			return trackingKey;
		}
        public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
        {
			return dependency.TargetType != null &&
			       dependency.TargetType.IsArray &&
			       dependency.TargetType.GetElementType().IsInterface &&
				   !model.Parameters.Contains(dependency.DependencyKey);
        }
		public void Inspect(IHandler handler, DependencyModel[] missingDependencies, IKernel kernel)
		{
			if (handlersChecked.Add(handler) == false)
			{
				return;
			}
			Debug.Assert(missingDependencies.Length > 0, "missingDependencies.Length > 0");

			message.AppendLine();
			message.AppendFormat("'{0}' is waiting for the following dependencies:", handler.ComponentModel.Name);
			message.AppendLine();
			foreach (var dependency in missingDependencies)
			{
				switch (dependency.DependencyType)
				{
					case DependencyType.Service:
						InspectServiceDependency(handler, dependency, kernel);
						break;
					case DependencyType.ServiceOverride:
						InspectServiceOverrideDependency(dependency, kernel);
						break;
					case DependencyType.Parameter:
						InspectParameterDependency(dependency);
						break;
				}
			}
		}
예제 #10
0
 public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
 {
     string componentName = null;
     if (!_knownDependencies.ContainsKey(dependency))
         componentName = dependency.DependencyKey;
     return _kernel.Resolve(componentName, dependency.TargetType);
 }
예제 #11
0
		public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model,
		                       DependencyModel dependency)
		{
			return dependency.TargetType == typeof(IBookStore) && contextHandlerResolver.CanResolve(context, contextHandlerResolver, model,
			                                                                                        new DependencyModel(typeof(IBookStore).FullName,
			                                                                                                            typeof(IBookStore), false));
		}
			public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model,
			                      DependencyModel dependency)
			{
				return contextHandlerResolver.Resolve(context, contextHandlerResolver, model,
				                                      new DependencyModel(DependencyType.Service, typeof(IBookStore).FullName,
				                                                          typeof(IBookStore), false));
			}
		public object Resolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model,
							  DependencyModel dependency)
		{
			Type elementType = dependency.TargetType.GetElementType();
			Array all = kernel.ResolveAll(elementType, new Hashtable());
			return all;
		}
        /// <inheritdoc />
        public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, Castle.Core.ComponentModel model, DependencyModel dependency)
        {
            if (dependency.TargetType == null)
            {
                return false;
            }

            Type genericArgument = null;
            if ((dependency.TargetType.IsGenericType) && (dependency.TargetType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
            {
                genericArgument = dependency.TargetType.GetGenericArguments()[0];
            }
            else
            {
                dependency.TargetType.GetInterfaces().Any(implemented => (implemented.IsGenericType) && (implemented.GetGenericTypeDefinition() == typeof(IEnumerable<>)) &&
                    ((genericArgument = implemented.GetGenericArguments()[0]) != null));
            }

            if ((genericArgument == null) || (!_kernel.HasComponent(genericArgument)))
            {
                return false;
            }

            return true;
        }
		protected virtual ConstructorCandidate CreateConstructorCandidate(ConstructorInfo constructor)
		{
			ParameterInfo[] parameters = constructor.GetParameters();

			DependencyModel[] dependencies = new DependencyModel[parameters.Length];

			for(int i = 0; i < parameters.Length; i++)
			{
				ParameterInfo parameter = parameters[i];

				Type paramType = parameter.ParameterType;

				// This approach is somewhat problematic. We should use
				// another strategy to differentiate types and classify dependencies
				if (converter.IsSupportedAndPrimitiveType(paramType))
				{
					dependencies[i] = new DependencyModel(
						DependencyType.Parameter, parameter.Name, paramType, false);
				}
				else
				{
					dependencies[i] = new DependencyModel(
						DependencyType.Service, parameter.Name, paramType, false);
				}
			}

			return new ConstructorCandidate(constructor, dependencies);
		}
        public bool CanResolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model,
                               DependencyModel dependency)
        {

            return Context.CurrentUser != null &&
                   dependency.TargetType == typeof (INotifications);
        }
예제 #17
0
        public Boolean IsValidFor( DependencyModel model )
        {
            if ( model.DependencyKey == DependencyName ||
                 model.TargetType == TypeKey )
                return true;

            return false;
        }
예제 #18
0
		public override object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver,
		                               ComponentModel model,
		                               DependencyModel dependency)
		{
			var items = base.Resolve(context, contextHandlerResolver, model, dependency);
			var listType = BuildListType(dependency);
			return listType.CreateInstance<object>(items);
		}
예제 #19
0
        public bool CanResolve(
			CreationContext context,
			ISubDependencyResolver parentResolver,
			ComponentModel model,
			DependencyModel dependency )
        {
            return dependency.DependencyType == DependencyType.Service;
        }
예제 #20
0
 public bool CanResolve(CreationContext context, ISubDependencyResolver parentResolver,
                        ComponentModel model,
                        DependencyModel dependency)
 {
     return dependency.TargetType != null &&
            dependency.TargetType.IsArray &&
            dependency.TargetType.GetElementType().IsInterface;
 }
 /// <summary>
 /// Returns true if the resolver is able to satisfy this dependency.
 /// </summary>
 /// <param name="context">
 /// Creation context, which is a resolver itself
 /// </param>
 /// <param name="contextHandlerResolver">
 /// Parent resolver - normally the IHandler implementation
 /// </param>
 /// <param name="model">
 /// Model of the component that is requesting the dependency
 /// </param>
 /// <param name="dependency">
 /// The dependency model
 /// </param>
 /// <returns>
 /// <c>true</c> if the dependency can be satisfied
 /// </returns>
 public bool CanResolve(
     CreationContext context, 
     ISubDependencyResolver contextHandlerResolver, 
     ComponentModel model, 
     DependencyModel dependency)
 {
     return ConfigurationManager.AppSettings.AllKeys.Contains(dependency.DependencyKey)
            && TypeDescriptor.GetConverter(dependency.TargetType).CanConvertFrom(typeof(string));
 }
예제 #22
0
		public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver,
		                       ComponentModel model,
		                       DependencyModel dependency)
		{
			var targetType = dependency.TargetItemType;
			return targetType != null &&
			       targetType.IsArray &&
			       (allowEmptyArray || kernel.HasComponent(targetType.GetElementType()));
		}
		private bool IsDuplicate(DependencyModel foo, DependencyModel bar)
		{
			if (foo.ReferencedComponentName != null && bar.ReferencedComponentName != null)
			{
				return string.Equals(foo.ReferencedComponentName, bar.ReferencedComponentName, StringComparison.OrdinalIgnoreCase);
			}

			return string.Equals(foo.DependencyKey, bar.DependencyKey, StringComparison.OrdinalIgnoreCase);
		}
 public object Resolve(CreationContext context,
     ISubDependencyResolver parentResolver,
     ComponentModel model,
     DependencyModel dependency)
 {
     var targetType = dependency.TargetType.GetElementType();
     var resolved = _kernel.ResolveAll(targetType, null);
     return resolved;
 }
 public virtual object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model,
                               DependencyModel dependency)
 {
     Array result = kernel.ResolveAll(GetItemType(dependency.TargetItemType), null);
     if (_orderFunc != null)
     {
         Array.Sort(result, new GenericComparer(_orderFunc));
     }
     return result;
 }
 public bool CanResolve(CreationContext context,
     ISubDependencyResolver parentResolver,
     ComponentModel model,
     DependencyModel dependency)
 {
     return dependency.TargetType != null &&
            dependency.TargetType.IsArray &&
            dependency.TargetType.GetElementType().IsInterface &&
            !model.Parameters.Any(x => x.Value == dependency.DependencyKey);
 }
        public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver,
                              ComponentModel model, DependencyModel dependency)
        {
            var candidateType = GetItemType(dependency.TargetItemType);
            var candidates = _kernel.ResolveAll(candidateType);

            var typeToConstruct = typeof (Registry<>).GetGenericTypeDefinition().MakeGenericType(new[] {candidateType});

            return Activator.CreateInstance(typeToConstruct, candidates);
        }
예제 #28
0
 public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
 {
     // This person is the 'currently logged-in user', i.e. the context needed to resolve which implementation
     // of ISecurityService needs to be returned
     Person person = kernel.Resolve<Person>();
     if (person.UserName == "richard")
     {
         return new InsecureSecurityService();
     }
     return new SecureSecurityService();
 }
 public object Resolve(
     CreationContext context,
     ISubDependencyResolver contextHandlerResolver,
     ComponentModel model,
     DependencyModel dependency)
 {
     return TypeDescriptor
         .GetConverter(dependency.TargetType)
         .ConvertFrom(
             ConfigurationManager.AppSettings[dependency.DependencyKey]);
 }
예제 #30
0
 public bool CanResolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency)
 {
     try
       {
     return _serviceInterfaceResolver.AttemptResolve(dependency.TargetType, _componentModelSource.Models) != null;
       }
       catch (Exception error)
       {
     throw new ServiceResolutionException(String.Format("Error resolving {0}", model.Name), error);
       }
 }
예제 #31
0
 /// <summary>
 ///   Initializes a new instance of the <see cref = "PropertySet" /> class.
 /// </summary>
 /// <param name = "propertyInfo">The property info.</param>
 /// <param name = "dependency">The dependency.</param>
 public PropertySet(PropertyInfo propertyInfo, DependencyModel dependency)
 {
     this.propertyInfo = propertyInfo;
     this.dependency   = dependency;
 }