Ejemplo n.º 1
0
 private object CreateInstance(IObjectConfiguration objectConfig)
 {
     if (objectConfig.InstanceValue is IFactoryConfiguration)
     {
         IFactoryConfiguration factoryConfig = objectConfig.InstanceValue as IFactoryConfiguration;
         return(factoryConfig.Invoke(this, null, InstanceMode.Default));
     }
     else
     {
         return(objectConfig.CreateObject(this));
     }
 }
        private IObjectConfiguration GetObjectConfiguration(string name, IContainer container)
        {
            IObjectConfiguration objectConfig = container.Configuration.GetObjectConfiguration(name);

            if (objectConfig == null)
            {
                objectConfig      = new ObjectConfiguration();
                objectConfig.Name = name;
                container.Configuration.AddObjectConfiguration(objectConfig);
            }

            return(objectConfig);
        }
Ejemplo n.º 3
0
        public virtual IObjectConfiguration GetConfiguration(string name)
        {
            IObjectConfiguration objectConfig = Configuration.GetObjectConfiguration(name);

            if (objectConfig == null)
            {
                if (ParentContainer == null)
                {
                    throw new Exception(string.Format("Configuration not found '{0}'", name));
                }
                else
                {
                    return(ParentContainer.GetConfiguration(name));
                }
            }
            return(objectConfig);
        }
Ejemplo n.º 4
0
        private void ConfigureObject(IObjectConfiguration objectConfig, object target)
        {
            ArrayList sortedPropertyConfigurations = new ArrayList(objectConfig.PropertyConfigurations);

            sortedPropertyConfigurations.Sort(new PropertyPathSorter());

            LogMessage message = new LogMessage("Configuring object '{0}' as '{1}'", target, objectConfig.Name);

            LogManager.Info(this, message);

            foreach (PropertyConfiguration propertyConfig in sortedPropertyConfigurations)
            {
                string   propertyPath = propertyConfig.Name;
                string[] properties   = propertyPath.Split('.');

                object       tmpTarget    = target;
                PropertyInfo propertyInfo = null;
                //traverse property path
                for (int i = 0; i < properties.Length; i++)
                {
                    if (i > 0)
                    {
                        tmpTarget = propertyInfo.GetValue(tmpTarget, null);
                    }

                    string property = properties[i];
                    Type   tmpType  = tmpTarget.GetType();
                    propertyInfo = tmpType.GetProperty(property);

                    if (propertyInfo == null)
                    {
                        throw new NullReferenceException(string.Format("Property path {0} was not found", propertyPath));
                    }
                }



                target = tmpTarget;
                propertyConfig.Type = propertyInfo.PropertyType;

                object res = propertyConfig.GetValue(this);
                if (propertyConfig.Value is IListConfiguration)
                {
                    IList orgList = (IList)propertyInfo.GetValue(target, null);

                    if (orgList != null)
                    {
                        IList resList = (IList)res;

                        if (propertyConfig.ListAction == ListAction.Replace)
                        {
                            orgList.Clear();
                        }

                        foreach (object item in resList)
                        {
                            orgList.Add(item);
                        }
                    }
                    else
                    {
                        propertyInfo.SetValue(target, res, null);
                    }
                }
                else
                {
                    if (propertyConfig.ListAction == ListAction.Add)
                    {
                        IList orgList = (IList)propertyInfo.GetValue(target, null);
                        orgList.Add(res);
                    }
                    else
                    {
                        propertyInfo.SetValue(target, res, null);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        public object GetObjectInternal(string name, InstanceMode instanceMode, IContainer rootContainer)
        {
            lock (syncRoot)
            {
                IObjectConfiguration objectConfig = Configuration.GetObjectConfiguration(name);

                if (rootContainer != null)
                {
                    IObjectConfiguration overrideConfig = rootContainer.GetConfiguration(name);

                    if (overrideConfig == objectConfig || overrideConfig == null)
                    {
                        //not overridden
                    }
                    else
                    {
                        objectConfig = overrideConfig;
                    }
                }



                if (objectConfig == null)
                {
                    if (ParentContainer == null)
                    {
                        throw new Exception(string.Format("Object not found '{0}'", name));
                    }
                    else
                    {
                        return(ParentContainer.GetObjectInternal(name, instanceMode, rootContainer));
                    }
                }

                if (instanceMode == InstanceMode.Default)
                {
                    instanceMode = objectConfig.InstanceMode;
                }

                if (instanceMode == InstanceMode.Default)
                {
                    instanceMode = InstanceMode.PerContainer;
                }

                //fetch object from container cache
                if (instanceMode == InstanceMode.PerContainer)
                {
                    //only fetch from cache if we dont need a new instance
                    if (containerObjects[name] != null)
                    {
                        return(containerObjects[name]);
                    }
                }

                //fetch object from graph cache
                if (instanceMode == InstanceMode.PerGraph)
                {
                    //only fetch from cache if we dont need a new instance
                    if (graphObjects[name] != null)
                    {
                        return(graphObjects[name]);
                    }
                }

                object instance = CreateInstance(objectConfig);

                //store instance in container cache
                if (instanceMode == InstanceMode.PerContainer)
                {
                    containerObjects[objectConfig.Name] = instance;
                }

                //store instance in graph cache
                if (instanceMode == InstanceMode.PerGraph)
                {
                    graphObjects[objectConfig.Name] = instance;
                }

                ConfigureObject(objectConfig, instance);

                return(instance);
            }
        }
        private void ConfigureElement(XmlNode node, ElementConfiguration config, IContainer container, Type valueType)
        {
            config.Type = valueType;

            if (node.Attributes["value"] != null)
            {
                string propertyValueString = node.Attributes["value"].Value;

                ValueConfiguration propertyValueConfig = new ValueConfiguration();
                propertyValueConfig.Value = propertyValueString;
                config.Value = propertyValueConfig;

                if (node.Attributes["type-converter"] != null)
                {
                    string typeConverterString = node.Attributes["type-converter"].Value;
                    Type   typeConverterType   = ResolveType(typeConverterString);

                    TypeConverter typeConverter = (TypeConverter)Activator.CreateInstance(typeConverterType);
                    propertyValueConfig.TypeConverter = typeConverter;
                }

                if (node.Attributes["type"] != null)
                {
                    string typeString = node.Attributes["type"].Value;
                    Type   type       = ResolveType(typeString);

                    config.Type = type;
                }
            }

            if (node.Attributes["object"] != null)
            {
                string propertyObjectName = node.Attributes["object"].Value;
                IObjectConfiguration propertyObjectConfig = GetObjectConfiguration(propertyObjectName, container);

                config.Value = propertyObjectConfig;

                //done
                if (node.Attributes["instance-mode"] != null)
                {
                    string instanceModeString = node.Attributes["instance-mode"].Value;
                    config.InstanceMode = (InstanceMode)InstanceMode.Parse(typeof(InstanceMode), instanceModeString);
                }
            }

            if (node.Attributes["list"] != null)
            {
                string             propertyListName   = node.Attributes["list"].Value;
                IListConfiguration propertyListConfig = GetListConfiguration(propertyListName, container);

                config.Value = propertyListConfig;
                config.Type  = typeof(IList);
            }

            if (node.Attributes["factory"] != null)
            {
                string itemFactoryName = node.Attributes["factory"].Value;
                IFactoryConfiguration propertyFactoryConfig = GetFactoryConfiguration(itemFactoryName, container);

                config.Value = propertyFactoryConfig;
            }
        }
        private IObjectConfiguration ConfigureObject(XmlNode configNode, IContainer container)
        {
            string objectName = configNode.Attributes["name"].Value;
            IObjectConfiguration objectConfig = GetObjectConfiguration(objectName, container);

            objectConfig.Name = objectName;


            if (configNode.Attributes["aop-config"] != null)
            {
                string  sectionName = configNode.Attributes["aop-config"].Value;
                IEngine engine      = NAspect.Framework.ApplicationContext.ConfigureFromSection(sectionName);
                objectConfig.AopEngine = engine;
            }

            if (configNode.Attributes["type"] != null)
            {
                string objectTypeString = configNode.Attributes["type"].Value;
                Type   objectType       = ResolveType(objectTypeString);
                objectConfig.Type = objectType;
            }

            if (configNode.Attributes["factory"] != null)
            {
                string factoryName = configNode.Attributes["factory"].Value;
                IFactoryConfiguration factoryConfig = GetFactoryConfiguration(factoryName, container);
                objectConfig.InstanceValue = factoryConfig;
            }


            //done
            if (configNode.Attributes["instance-mode"] != null)
            {
                string instanceModeString = configNode.Attributes["instance-mode"].Value;
                objectConfig.InstanceMode = (InstanceMode)InstanceMode.Parse(typeof(InstanceMode), instanceModeString);
            }

            container.Configuration.AddObjectConfiguration(objectConfig);

            foreach (XmlNode objectNode in configNode)
            {
                #region property

                if (objectNode.Name == "property")
                {
                    PropertyConfiguration propertyConfig = new PropertyConfiguration();
                    propertyConfig.Name = objectNode.Attributes["name"].Value;
                    ConfigureElement(objectNode, propertyConfig, container, null);
                    if (objectNode.Attributes["action"] != null)
                    {
                        string action = objectNode.Attributes["action"].Value;
                        if (action == "Add")
                        {
                            propertyConfig.ListAction = ListAction.Add;
                        }

                        if (action == "Replace")
                        {
                            propertyConfig.ListAction = ListAction.Replace;
                        }
                    }

                    objectConfig.PropertyConfigurations.Add(propertyConfig);
                }

                #endregion

                #region Ctor Parameter

                if (objectNode.Name == "ctor-parameter")
                {
                    ParameterConfiguration parameterConfig = new ParameterConfiguration();
                    parameterConfig.Index = Convert.ToInt32(objectNode.Attributes["index"].Value);
                    ConfigureElement(objectNode, parameterConfig, container, null);
                    objectConfig.CtorParameterConfigurations.Add(parameterConfig);
                }

                #endregion
            }
            return(objectConfig);
        }
        private IFactoryConfiguration ConfigureFactory(XmlNode configNode, IContainer container)
        {
            string factoryName                  = configNode.Attributes["name"].Value;
            string factoryMethodName            = configNode.Attributes["method"].Value;
            IFactoryConfiguration factoryConfig = GetFactoryConfiguration(factoryName, container);

            factoryConfig.Name       = factoryName;
            factoryConfig.MethodName = factoryMethodName;


            if (configNode.Attributes["type"] != null)             //
            {
                string objectTypeString = configNode.Attributes["type"].Value;
                Type   objectType       = ResolveType(objectTypeString);

                factoryConfig.Type = objectType;
            }
            else if (configNode.Attributes["object"] != null)             //instance
            {
                string objectName = configNode.Attributes["object"].Value;
                IObjectConfiguration objectConfig = GetObjectConfiguration(objectName, container);
                factoryConfig.Object = objectConfig;
            }

            foreach (XmlNode factoryNode in configNode)
            {
                #region Parameter

                if (factoryNode.Name == "parameter")
                {
                    ParameterConfiguration parameterConfig = new ParameterConfiguration();
                    parameterConfig.Index = Convert.ToInt32(factoryNode.Attributes["index"].Value);


                    if (factoryNode.Attributes["value"] != null)
                    {
                        string             propertyValueString = factoryNode.Attributes["value"].Value;
                        ValueConfiguration propertyValueConfig = new ValueConfiguration();
                        if (factoryNode.Attributes["type"] != null)
                        {
                            //typed parameter
                            string parameterTypeString = factoryNode.Attributes["type"].Value;
                            Type   parameterType       = ResolveType(parameterTypeString);
                            parameterConfig.Type      = parameterType;
                            propertyValueConfig.Value = Convert.ChangeType(propertyValueString, parameterConfig.Type);
                        }
                        else
                        {
                            //untyped parameter
                            propertyValueConfig.Value = propertyValueString;
                            //		parameterConfig.UntypedStringValue = propertyValueString;
                            parameterConfig.Type = null;
                        }

                        parameterConfig.Value = propertyValueConfig;
                    }

                    if (factoryNode.Attributes["object"] != null)
                    {
                        string parameterObjectName = factoryNode.Attributes["object"].Value;
                        IObjectConfiguration propertyObjectConfig = GetObjectConfiguration(parameterObjectName, container);

                        parameterConfig.Value = propertyObjectConfig;

                        //done
                        if (factoryNode.Attributes["instance-mode"] != null)
                        {
                            string instanceModeString = factoryNode.Attributes["instance-mode"].Value;
                            parameterConfig.InstanceMode = (InstanceMode)InstanceMode.Parse(typeof(InstanceMode), instanceModeString);
                        }
                    }

                    factoryConfig.ParameterConfigurations.Add(parameterConfig);
                }

                #endregion
            }

            return(factoryConfig);
        }
        public IContainer Configure(XmlElement xmlRoot)
        {
            IContainer container = new Container();

            XmlElement o = xmlRoot;

            if (o == null)
            {
                return(container);
            }

            ArrayList objectConfigurations  = new ArrayList();
            ArrayList listConfigurations    = new ArrayList();
            ArrayList factoryConfigurations = new ArrayList();


            foreach (XmlNode configNode in o)
            {
                if (configNode.Name == "object")
                {
                    IObjectConfiguration objectConfig = ConfigureObject(configNode, container);

                    objectConfigurations.Add(objectConfig);
                }

                if (configNode.Name == "list")
                {
                    IListConfiguration listConfig = ConfigureList(configNode, container);

                    listConfigurations.Add(listConfig);
                }

                if (configNode.Name == "factory")
                {
                    IFactoryConfiguration factoryConfig = ConfigureFactory(configNode, container);

                    factoryConfigurations.Add(factoryConfig);
                }
            }

            foreach (FactoryConfiguration factoryConfig in factoryConfigurations)
            {
                FillFactoryParameterTypes(factoryConfig);
                if (factoryConfig.Object != null)
                {
                    factoryConfig.Type = factoryConfig.Object.Type;
                }

                bool       isStatic        = factoryConfig.Object == null;
                MethodInfo bestMethodMatch = MatchMethods(factoryConfig, factoryConfig.Type, factoryConfig.MethodName, isStatic)
                ;
                if (bestMethodMatch == null)
                {
                    throw new Exception(string.Format("Can not find Method for type '{0}' that matches your parameters", factoryConfig.Type));
                }
                factoryConfig.Method = bestMethodMatch;
            }


            foreach (ObjectConfiguration objectConfig in objectConfigurations)
            {
                FillCtorParameterTypes(objectConfig);

                if (objectConfig.InstanceValue is FactoryConfiguration)
                {
                    FactoryConfiguration factory = objectConfig.InstanceValue as FactoryConfiguration;
                    objectConfig.Type = factory.Method.ReturnType;
                }
                else
                {
                    ConstructorInfo bestCtorMatch = MatchConstructors(objectConfig);
                    if (bestCtorMatch == null)
                    {
                        throw new Exception(string.Format("Can not find CTOR for type '{0}' that matches your parameters", objectConfig.Type));
                    }
                    objectConfig.Constructor = bestCtorMatch;
                }
            }

            foreach (ListConfiguration listConfig in listConfigurations)
            {
                FillItemTypes(listConfig);
            }


            //cache configuration


            return(container);
        }
 public void AddObjectConfiguration(IObjectConfiguration config)
 {
     this.objectConfigurations[config.Name] = config;
 }
 public void AddObjectConfiguration(IObjectConfiguration config)
 {
     this.objectConfigurations[config.Name] = config;
 }