private void LoadObjectDefinitionsForModelMethod(ConfigurationClassMethod method)
        {
            ConfigurationClass configClass = method.ConfigurationClass;
            MethodInfo metadata = method.MethodMetadata;

            RootObjectDefinition objDef = new ConfigurationClassObjectDefinition();

            objDef.FactoryObjectName = configClass.ObjectName;
            objDef.FactoryMethodName = metadata.Name;
            objDef.AutowireMode = Objects.Factory.Config.AutoWiringMode.Constructor;

            // consider name and any aliases
            //Dictionary<String, Object> ObjectAttributes = metadata.getAnnotationAttributes(Object.class.getName());
            object[] objectAttributes = metadata.GetCustomAttributes(typeof(DefinitionAttribute), true);
            List<string> names = new List<string>();
            foreach (object t in objectAttributes)
            {
                string[] namesAndAliases = ((DefinitionAttribute)t).NamesToArray;

                if (namesAndAliases != null)
                {
                    names.Add(metadata.Name);
                }
                else
                {
                    namesAndAliases = new[] { metadata.Name };
                }

                names.AddRange(namesAndAliases);
            }

            string objectName = (names.Count > 0 ? names[0] : method.MethodMetadata.Name);
            for (int i = 1; i < names.Count; i++)
            {
                _registry.RegisterAlias(objectName, names[i]);
            }

            // has this already been overridden (e.g. via XML)?
            if (_registry.ContainsObjectDefinition(objectName))
            {
                IObjectDefinition existingObjectDef = _registry.GetObjectDefinition(objectName);
                // is the existing Object definition one that was created from a configuration class?
                if (!(existingObjectDef is ConfigurationClassObjectDefinition))
                {
                    // no -> then it's an external override, probably XML
                    // overriding is legal, return immediately
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug(
                            String.Format("Skipping loading Object definition for {0}: a definition for object " +
                                          "'{1}' already exists. This is likely due to an override in XML.", method,
                                          objectName));
                    }
                    return;
                }
            }

            //TODO: container does not presently support the concept of Primary object defintion for type resolution
            //if (Attribute.GetCustomAttribute(metadata, typeof(PrimaryAttribute)) != null)
            //{
            //    ObjectDef.isPrimary = true;
            //}

            // is this Object to be instantiated lazily?
            if (Attribute.GetCustomAttribute(metadata, typeof(LazyAttribute)) != null)
            {
                objDef.IsLazyInit =
                    (Attribute.GetCustomAttribute(metadata, typeof(LazyAttribute)) as LazyAttribute).LazyInitialize;
            }

            if (Attribute.GetCustomAttribute(metadata, typeof(DependsOnAttribute)) != null)
            {
                objDef.DependsOn =
                    (Attribute.GetCustomAttribute(metadata, typeof(DependsOnAttribute)) as DependsOnAttribute).Name;
            }

            //TODO: container does not presently support autowiring to the degree needed to support this feature as of yet
            //Autowire autowire = (Autowire) ObjectAttributes.get("autowire");
            //if (autowire.isAutowire()) {
            //	ObjectDef.setAutowireMode(autowire.value());
            //}

            if (Attribute.GetCustomAttribute(metadata, typeof(DefinitionAttribute)) != null)
            {
                objDef.InitMethodName =
                    (Attribute.GetCustomAttribute(metadata, typeof(DefinitionAttribute)) as DefinitionAttribute).
                        InitMethod;
                objDef.DestroyMethodName =
                    (Attribute.GetCustomAttribute(metadata, typeof(DefinitionAttribute)) as DefinitionAttribute).
                        DestroyMethod;
            }

            // consider scoping
            if (Attribute.GetCustomAttribute(metadata, typeof(ScopeAttribute)) != null)
            {
                objDef.Scope =
                    (Attribute.GetCustomAttribute(metadata, typeof(ScopeAttribute)) as ScopeAttribute).ObjectScope.ToString();
            }

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug(String.Format("Registering Object definition for [Definition] method {0}.{1}()",
                                            configClass.ConfigurationClassType.Name, objectName));
            }

            _registry.RegisterObjectDefinition(objectName, objDef);
        }
Exemple #2
0
        private void LoadObjectDefinitionsForModelMethod(ConfigurationClassMethod method)
        {
            ConfigurationClass configClass = method.ConfigurationClass;
            MethodInfo         metadata    = method.MethodMetadata;

            RootObjectDefinition objDef = new ConfigurationClassObjectDefinition();

            objDef.FactoryObjectName = configClass.ObjectName;
            objDef.FactoryMethodName = metadata.Name;
            objDef.AutowireMode      = Objects.Factory.Config.AutoWiringMode.Constructor;

            // consider name and any aliases
            //Dictionary<String, Object> ObjectAttributes = metadata.getAnnotationAttributes(Object.class.getName());
            object[]      objectAttributes = metadata.GetCustomAttributes(typeof(ObjectDefAttribute), true);
            List <string> names            = new List <string>();

            foreach (object t in objectAttributes)
            {
                string[] namesAndAliases = ((ObjectDefAttribute)t).NamesToArray;

                if (namesAndAliases != null)
                {
                    names.Add(metadata.Name);
                }
                else
                {
                    namesAndAliases = new[] { metadata.Name };
                }

                names.AddRange(namesAndAliases);
            }

            string objectName = (names.Count > 0 ? names[0] : method.MethodMetadata.Name);

            for (int i = 1; i < names.Count; i++)
            {
                _registry.RegisterAlias(objectName, names[i]);
            }

            // has this already been overridden (e.g. via XML)?
            if (_registry.ContainsObjectDefinition(objectName))
            {
                IObjectDefinition existingObjectDef = _registry.GetObjectDefinition(objectName);
                // is the existing Object definition one that was created from a configuration class?
                if (!(existingObjectDef is ConfigurationClassObjectDefinition))
                {
                    // no -> then it's an external override, probably XML
                    // overriding is legal, return immediately
                    Logger.Debug(m => m("Skipping loading Object definition for {0}: a definition for object " +
                                        "'{1}' already exists. This is likely due to an override in XML.", method,
                                        objectName));
                    return;
                }
            }

            //TODO: container does not presently support the concept of Primary object definition for type resolution
            //if (Attribute.GetCustomAttribute(metadata, typeof(PrimaryAttribute)) != null)
            //{
            //    ObjectDef.isPrimary = true;
            //}

            // is this Object to be instantiated lazily?
            if (Attribute.GetCustomAttribute(metadata, typeof(LazyAttribute)) != null)
            {
                objDef.IsLazyInit =
                    (Attribute.GetCustomAttribute(metadata, typeof(LazyAttribute)) as LazyAttribute).LazyInitialize;
            }

            if (Attribute.GetCustomAttribute(metadata, typeof(DependsOnAttribute)) != null)
            {
                objDef.DependsOn =
                    (Attribute.GetCustomAttribute(metadata, typeof(DependsOnAttribute)) as DependsOnAttribute).Name;
            }

            //TODO: container does not presently support autowiring to the degree needed to support this feature as of yet
            //Autowire autowire = (Autowire) ObjectAttributes.get("autowire");
            //if (autowire.isAutowire()) {
            //	ObjectDef.setAutowireMode(autowire.value());
            //}

            if (Attribute.GetCustomAttribute(metadata, typeof(ObjectDefAttribute)) != null)
            {
                objDef.InitMethodName =
                    (Attribute.GetCustomAttribute(metadata, typeof(ObjectDefAttribute)) as ObjectDefAttribute).
                    InitMethod;
                objDef.DestroyMethodName =
                    (Attribute.GetCustomAttribute(metadata, typeof(ObjectDefAttribute)) as ObjectDefAttribute).
                    DestroyMethod;
            }

            // consider scoping
            if (Attribute.GetCustomAttribute(metadata, typeof(ScopeAttribute)) != null)
            {
                objDef.Scope =
                    (Attribute.GetCustomAttribute(metadata, typeof(ScopeAttribute)) as ScopeAttribute).ObjectScope.ToString();
            }

            Logger.Debug(m => m("Registering Object definition for [ObjectDef] method {0}.{1}()",
                                configClass.ConfigurationClassType.Name, objectName));

            _registry.RegisterObjectDefinition(objectName, objDef);
        }