Пример #1
0
 /// <summary>
 ///   Invoked by
 ///   <see cref = "EnsureDependenciesCanBeSatisfied" />
 ///   in order to check if a dependency can be satisfied.
 ///   If not, the handler is set to a 'waiting dependency' state.
 /// </summary>
 /// <remarks>
 ///   This method registers the dependencies within the correct collection
 ///   or dictionary and changes the handler state to
 ///   <see cref = "HandlerState.WaitingDependency" />
 /// </remarks>
 /// <param name = "dependency"></param>
 protected void AddDependency(DependencyModel dependency)
 {
     dependency.Init(model.ParametersInternal);
     if (AddOptionalDependency(dependency))
     {
         return;
     }
     if (AddResolvableDependency(dependency))
     {
         return;
     }
     AddMissingDependency(dependency);
 }
Пример #2
0
        private object Create(object factoryInstance, string factoryId, MethodInfo instanceCreateMethod, string factoryCreate, CreationContext context)
        {
            object instance;
            var    methodArgs = new List <object>();

            try
            {
                var parameters = instanceCreateMethod.GetParameters();

                foreach (var parameter in parameters)
                {
                    var paramType = parameter.ParameterType;

                    if (paramType == typeof(IKernel))
                    {
                        methodArgs.Add(Kernel);
                        continue;
                    }
                    if (paramType == typeof(CreationContext))
                    {
                        methodArgs.Add(context);
                        continue;
                    }

                    var dependency = new DependencyModel(parameter.Name, paramType, false);
                    dependency.Init(Model.HasParameters ? Model.Parameters : null);
                    if (!Kernel.Resolver.CanResolve(context, null, Model, dependency))
                    {
                        var message = String.Format(
                            "Factory Method {0}.{1} requires an argument '{2}' that could not be resolved",
                            instanceCreateMethod.DeclaringType.FullName, instanceCreateMethod.Name, parameter.Name);
                        throw new FacilityException(message);
                    }

                    var arg = Kernel.Resolver.Resolve(context, null, Model, dependency);

                    methodArgs.Add(arg);
                }

                instance = instanceCreateMethod.Invoke(factoryInstance, methodArgs.ToArray());
            }
            catch (Exception ex)
            {
                var message = String.Format("You have specified a factory " +
                                            "('{2}' - method to be called: {3}) " +
                                            "for the component '{0}' {1} that failed during invoke.",
                                            Model.Name, Model.Implementation.FullName, factoryId, factoryCreate);

                throw new FacilityException(message, ex);
            }

            if (Model.HasInterceptors)
            {
                try
                {
                    instance = Kernel.ProxyFactory.Create(Kernel, instance, Model, context, methodArgs.ToArray());
                }
                catch (Exception ex)
                {
                    throw new ComponentActivatorException("FactoryActivator: could not proxy " + instance.GetType().FullName, ex, Model);
                }
            }

            return(instance);
        }