Esempio n. 1
0
        public void Attach(ComponentModel component)
        {
            var reference = ReferenceExpressionUtil.BuildReference(ServiceOverrideComponentName ?? ActualComponentType.FullName);

            component.Parameters.Add(dependency.DependencyKey, reference);
            component.Dependencies.Add(dependency);
        }
Esempio n. 2
0
        protected virtual void CollectFromConfiguration(ComponentModel model)
        {
            if (model.Configuration == null)
            {
                return;
            }

            IConfiguration interceptors = model.Configuration.Children["interceptors"];

            if (interceptors == null)
            {
                return;
            }

            foreach (IConfiguration interceptor in interceptors.Children)
            {
                String value = interceptor.Value;

                if (!ReferenceExpressionUtil.IsReference(value))
                {
                    String message = String.Format(
                        "The value for the interceptor must be a reference " +
                        "to a component (Currently {0})",
                        value);

                    throw new ConfigurationException(message);
                }

                InterceptorReference interceptorRef = new InterceptorReference(ReferenceExpressionUtil.ExtractComponentKey(value));

                model.Interceptors.Add(interceptorRef);
                model.Dependencies.Add(CreateDependencyModel(interceptorRef));
            }
        }
Esempio n. 3
0
        void IReference <IInterceptor> .Attach(ComponentModel component)
        {
            var reference = ReferenceExpressionUtil.BuildReference(serviceOverrideComponent ?? serviceType.FullName);

            component.Parameters.Add(dependencyModel.DependencyKey, reference);
            component.Dependencies.Add(dependencyModel);
        }
Esempio n. 4
0
 public override bool CanHandleType(Type type, IConfiguration configuration)
 {
     if (configuration.Value != null)
     {
         return(ReferenceExpressionUtil.IsReference(configuration.Value));
     }
     return(CanHandleType(type));
 }
Esempio n. 5
0
 private void AddSubscriberDependecyToModel(IEnumerable <string> subscribers, ComponentModel model)
 {
     foreach (var subscriber in subscribers)
     {
         var name      = "event-subscription-" + subscriber;
         var reference = ReferenceExpressionUtil.BuildReference(subscriber);
         model.Parameters.Add(name, reference);
         model.Dependencies.Add(new DependencyModel(name, null, false));
     }
 }
        /// <summary>
        /// Extracts the component name from the a ref strings which is
        /// ${something}
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        protected virtual String ExtractComponentKey(String keyValue, String name)
        {
            if (!ReferenceExpressionUtil.IsReference(keyValue))
            {
                throw new DependencyResolverException(
                          String.Format("Key invalid for parameter {0}. " +
                                        "Thus the kernel was unable to override the service dependency", name));
            }

            return(ReferenceExpressionUtil.ExtractComponentKey(keyValue));
        }
Esempio n. 7
0
        private void AssertHasDependency <TComponnet>(string name)
        {
            var handler   = GetHandler <TComponnet>();
            var reference = ReferenceExpressionUtil.BuildReference(name);
            var parameter = handler.ComponentModel.Parameters.FirstOrDefault(p => p.Value == reference);

            Assert.IsNotNull(parameter, "Parameter for dependency '{0}' should exist", name);

            var dependency = handler.ComponentModel.Dependencies.SingleOrDefault(d => d.DependencyKey == parameter.Name);

            Assert.IsNotNull(dependency, "Parameter named '{1}' for dependency on '{0}' should exist.", name, parameter.Name);
        }
        public void Init(ParameterModelCollection parameters)
        {
#if DEBUG
            initialized = true;
#endif
            if (parameters == null)
            {
                return;
            }
            parameter = ObtainParameterModelByName(parameters) ?? ObtainParameterModelByType(parameters);
            if (parameter != null)
            {
                reference = ReferenceExpressionUtil.ExtractComponentKey(parameter.Value);
            }
        }
Esempio n. 9
0
        public override object PerformConversion(String value, Type targetType)
        {
            if (ReferenceExpressionUtil.IsReference(value))
            {
                string newValue = ReferenceExpressionUtil.ExtractComponentName(value);
                var    handler  = Context.Kernel.LoadHandlerByName(newValue, targetType, null);
                if (handler == null)
                {
                    throw new ConverterException(string.Format("Component '{0}' was not found in the container.", newValue));
                }

                return(handler.Resolve(Context.CurrentCreationContext ?? CreationContext.CreateEmpty()));
            }
            throw new NotImplementedException();
        }
Esempio n. 10
0
        private void CollectInterceptors(ComponentModel model, IConfiguration interceptors)
        {
            foreach (var interceptor in interceptors.Children)
            {
                var interceptorComponent = ReferenceExpressionUtil.ExtractComponentName(interceptor.Value);
                if (interceptorComponent == null)
                {
                    throw new Exception(
                              String.Format("The value for the interceptor must be a reference to a component (Currently {0})", interceptor.Value));
                }

                var reference = new InterceptorReference(interceptorComponent);

                model.Interceptors.Add(reference);
            }
        }
Esempio n. 11
0
        // registers factory from configuration
        protected void RegisterFactory(string id, Type type, string selector)
        {
            var factory = Component.For(type).Named(id);

            if (selector == null)
            {
                factory.AsFactory();
            }
            else
            {
                var selectorKey = ReferenceExpressionUtil.ExtractComponentKey(selector);
                factory.AsFactory(x => x.SelectedWith(selectorKey));
            }

            Kernel.Register(factory);
        }
        public virtual void ProcessModel(IKernel kernel, ComponentModel model)
        {
            if (model.Configuration == null)
            {
                return;
            }

            IConfiguration parameters = model.Configuration.Children["parameters"];

            if (parameters == null)
            {
                return;
            }

            foreach (IConfiguration parameter in parameters.Children)
            {
                String name  = parameter.Name;
                String value = parameter.Value;

                if (value == null && parameter.Children.Count != 0)
                {
                    IConfiguration parameterValue = parameter.Children[0];
                    model.Parameters.Add(name, parameterValue);
                }
                else
                {
                    if (parameter.Attributes["type"] == "static")
                    {
                        value = StaticReflectionHelper.GetStaticValue <string>(parameter.Value);
                    }

                    model.Parameters.Add(name, value);
                }
            }

            foreach (ParameterModel parameter in model.Parameters)
            {
                if (parameter.Value == null || !ReferenceExpressionUtil.IsReference(parameter.Value))
                {
                    continue;
                }

                String newKey = ReferenceExpressionUtil.ExtractComponentKey(parameter.Value);

                model.Dependencies.Add(new DependencyModel(DependencyType.ServiceOverride, newKey, null, false));
            }
        }
        private void AddAnyServiceOverrides(ComponentModel model, IConfiguration config)
        {
            foreach (var item in config.Children)
            {
                if (item.Children.Count > 0)
                {
                    AddAnyServiceOverrides(model, item);
                }

                var newKey = ReferenceExpressionUtil.ExtractComponentKey(item.Value);
                if (newKey == null)
                {
                    continue;
                }
                model.Dependencies.Add(new DependencyModel(newKey, null, false));
            }
        }
        private void AddAnyServiceOverrides(ComponentModel model, IConfiguration config, ParameterModel parameter)
        {
            foreach (var item in config.Children)
            {
                if (item.Children.Count > 0)
                {
                    AddAnyServiceOverrides(model, item, parameter);
                }

                var componentName = ReferenceExpressionUtil.ExtractComponentName(item.Value);
                if (componentName == null)
                {
                    continue;
                }
                model.Dependencies.Add(new ComponentDependencyModel(componentName));
            }
        }
        private void ApplyReferenceList(ComponentModel model, object key, IEnumerable <String> items, ServiceOverride serviceOverride)
        {
            var list = new MutableConfiguration("list");

            if (serviceOverride != null && serviceOverride.Type != null)
            {
                list.Attributes.Add("type", serviceOverride.Type.AssemblyQualifiedName);
            }

            foreach (var item in items)
            {
                var reference = ReferenceExpressionUtil.BuildReference(item);
                list.Children.Add(new MutableConfiguration("item", reference));
            }

            AddParameter(model, GetKeyString(key), list);
        }
        private void InspectCollections(ComponentModel model)
        {
            foreach (var parameter in model.Parameters)
            {
                if (parameter.ConfigValue != null)
                {
                    if (IsArray(parameter) || IsList(parameter))
                    {
                        AddAnyServiceOverrides(model, parameter.ConfigValue, parameter);
                    }
                }

                if (ReferenceExpressionUtil.IsReference(parameter.Value))
                {
                    model.Dependencies.Add(new DependencyModel(parameter.Name, null, false));
                }
            }
        }
Esempio n. 17
0
        public override object PerformConversion(String value, Type targetType)
        {
            var componentName = ReferenceExpressionUtil.ExtractComponentKey(value);

            if (componentName == null)
            {
                throw new ConverterException(string.Format("Could not convert expression '{0}' to type '{1}'. Expecting a reference override like ${{some key}}", value,
                                                           targetType.FullName));
            }

            var handler = Context.Kernel.LoadHandlerByKey(componentName, targetType, null);

            if (handler == null)
            {
                throw new ConverterException(string.Format("Component '{0}' was not found in the container.", componentName));
            }

            return(handler.Resolve(Context.CurrentCreationContext ?? CreationContext.CreateEmpty()));
        }
Esempio n. 18
0
        protected virtual void CollectSelector(IConfiguration interceptors, ProxyOptions options)
        {
            var selector = interceptors.Attributes["selector"];

            if (selector == null)
            {
                return;
            }

            var selectorComponent = ReferenceExpressionUtil.ExtractComponentName(selector);

            if (selectorComponent == null)
            {
                throw new Exception(
                          String.Format("The value for the selector must be a reference to a component (Currently {0})", selector));
            }

            options.Selector = new ComponentReference <IInterceptorSelector>(selectorComponent);
        }
Esempio n. 19
0
        protected virtual void CollectHook(IConfiguration interceptors, ProxyOptions options)
        {
            var hook = interceptors.Attributes["hook"];

            if (hook == null)
            {
                return;
            }

            var hookComponent = ReferenceExpressionUtil.ExtractComponentName(hook);

            if (hookComponent == null)
            {
                throw new Exception(
                          String.Format("The value for the hook must be a reference to a component (Currently {0})", hook));
            }

            options.Hook = new ComponentReference <IProxyGenerationHook>(hookComponent);
        }
    public void ProcessModel(IKernel kernel, ComponentModel model)
    {
        // or whatever other condition to bail out quickly
        if (model.Implementation.Name.EndsWith("Controller") == false)
        {
            return;
        }

        foreach (var constructor in model.Constructors)
        {
            foreach (var dependency in constructor.Dependencies)
            {
                if (dependency.TargetItemType != typeof(ICommand))
                {
                    continue;
                }
                dependency.Parameter = new ParameterModel(dependency.DependencyKey,
                                                          ReferenceExpressionUtil.BuildReference(dependency.DependencyKey));
            }
        }
    }
        private static IReference <IProxyGenerationHook> ObtainProxyHook(IKernel kernel, IConfiguration config)
        {
            IProxyGenerationHook hook = null;

            if (config != null)
            {
                var hookAttrib = config.Attributes[Constants.ControlProxyHookAttrib];

                if (hookAttrib != null)
                {
                    var hookComponent = ReferenceExpressionUtil.ExtractComponentKey(hookAttrib);
                    if (hookComponent != null)
                    {
                        return(new ComponentReference <IProxyGenerationHook>("synchronize-proxy-generation-hook", hookComponent));
                    }

                    var converter = kernel.GetConversionManager();
                    var hookType  = converter.PerformConversion <Type>(hookAttrib);

                    if (hookType.Is <IProxyGenerationHook>() == false)
                    {
                        var message = String.Format("The specified controlProxyHook does " +
                                                    "not implement the interface {1}. Type {0}",
                                                    hookType.FullName, typeof(IProxyGenerationHook).Name);

                        throw new ConfigurationErrorsException(message);
                    }

                    hook = hookType.CreateInstance <IProxyGenerationHook>();
                }
            }

            if (hook == null)
            {
                hook = SynchronizeProxyHook.Instance;
            }

            return(new InstanceReference <IProxyGenerationHook>(hook));
        }
Esempio n. 22
0
        public void ProcessModel(IKernel kernel, ComponentModel model)
        {
            if (model.Configuration == null)
            {
                return;
            }

            var mixins = model.Configuration.Children["mixins"];

            if (mixins == null)
            {
                return;
            }

            var mixinReferences = new List <ComponentReference <object> >();

            foreach (var mixin in mixins.Children)
            {
                var value = mixin.Value;

                var mixinComponent = ReferenceExpressionUtil.ExtractComponentKey(value);
                if (mixinComponent == null)
                {
                    throw new Exception(
                              String.Format("The value for the mixin must be a reference to a component (Currently {0})", value));
                }

                mixinReferences.Add(new ComponentReference <object>("mixin-" + mixinComponent, mixinComponent));
            }
            if (mixinReferences.Count == 0)
            {
                return;
            }
            var options = ProxyUtil.ObtainProxyOptions(model, true);

            mixinReferences.ForEach(options.AddMixinReference);
        }
        private void ApplySimpleReference(ComponentModel model, object dependencyKey, String componentKey)
        {
            var reference = ReferenceExpressionUtil.BuildReference(componentKey);

            AddParameter(model, GetKeyString(dependencyKey), reference);
        }