Beispiel #1
0
        /// <summary>
        /// Creates an instance using assembly qualified name and instancing mode. When using InstancingMode.Singleton the instance is registered in the instance container for next uses.
        /// </summary>
        ///
        /// <typeparam name="T">
        /// Type of the instance. Should be an interface.
        /// </typeparam>
        ///
        /// <param name="asssemblyQualifiedName">
        /// Assembly qualified name ("Namespace.TypeName, AssemblyName").
        /// </param>
        ///
        /// <param name="instancingMode">
        /// Instancing mode enumeration.
        /// </param>
        ///
        /// <param name="parameters">
        /// An array of arguments that match in number, order, and type the parameters of the constructor to invoke. If args is an empty array or null, the constructor that takes no parameters (the default constructor) is invoked.
        /// </param>
        ///
        /// <returns>
        /// The created instance.
        /// </returns>
        public T CreateInstance <T>(string asssemblyQualifiedName, InstancingMode instancingMode, params object[] parameters)
        {
            object instance     = null;
            Type   instanceType = Type.GetType(asssemblyQualifiedName, true);

            if (instancingMode == InstancingMode.Singleton)
            {
                if (!_container.ContainsKey(asssemblyQualifiedName))
                {
                    instance = Activator.CreateInstance(instanceType, parameters);
                    _container.TryAdd(asssemblyQualifiedName, instance);
                }
                else
                {
                    instance = _container[asssemblyQualifiedName];
                }
            }

            if (instancingMode == InstancingMode.NewInstance)
            {
                instance = Activator.CreateInstance(instanceType, parameters);
            }

            return((T)instance);
        }
Beispiel #2
0
        /// <summary>
        /// Creates an instance. When using InstancingMode.Singleton the instance is registered in the instance container for next uses.
        /// </summary>
        ///
        /// <typeparam name="T">
        /// Type of the instance. Should be a concrete class.
        /// </typeparam>
        ///
        /// <param name="instancingMode">
        /// Instancing mode enumeration.
        /// </param>
        ///
        /// <param name="parameters">
        /// An array of arguments that match in number, order, and type the parameters of the constructor to invoke. If args is an empty array or null, the constructor that takes no parameters (the default constructor) is invoked.
        /// </param>
        ///
        /// <returns>
        /// The created instance.
        /// </returns>
        public T CreateInstance <T>(InstancingMode instancingMode, params object[] parameters) where T : class, new()
        {
            object instance = null;

            if (instancingMode == InstancingMode.Singleton)
            {
                string key = string.Format("{0}.{1}", typeof(T).Namespace, typeof(T).Name);

                if (!_container.ContainsKey(key))
                {
                    instance = (T)Activator.CreateInstance(typeof(T), parameters, null);
                    _container.TryAdd(key, instance);
                }
                else
                {
                    instance = (T)_container[key];
                }
            }

            if (instancingMode == InstancingMode.NewInstance)
            {
                instance = (T)Activator.CreateInstance(typeof(T), null, null);
            }

            return((T)instance);
        }
Beispiel #3
0
        /// <summary>
        /// Creates an instance using instance type and instancing mode. When using InstancingMode.Singleton the instance is registered in the instance container for next uses.
        /// </summary>
        ///
        /// <typeparam name="T">
        /// Type of the instance. Should be an interface.
        /// </typeparam>
        ///
        /// <param name="instanceType">
        /// Instance type to create.
        /// </param>
        ///
        /// <param name="instancingMode">
        /// Instancing mode enumeration.
        /// </param>
        ///
        /// <param name="parameters">
        /// An array of arguments that match in number, order, and type the parameters of the constructor to invoke. If args is an empty array or null, the constructor that takes no parameters (the default constructor) is invoked.
        /// </param>
        ///
        /// <returns>
        /// The created instance.
        /// </returns>
        public T CreateInstance <T>(Type instanceType, InstancingMode instancingMode, params object[] parameters)
        {
            object instance = null;

            if (instancingMode == InstancingMode.Singleton)
            {
                if (!_container.ContainsKey(instanceType.FullName))
                {
                    instance = Activator.CreateInstance(instanceType, parameters);
                    _container.TryAdd(instanceType.FullName, instance);
                }
                else
                {
                    instance = _container[instanceType.FullName];
                }
            }

            if (instancingMode == InstancingMode.NewInstance)
            {
                instance = Activator.CreateInstance(instanceType, parameters);
            }

            return((T)instance);
        }
 public NetHttpInstancingModeAttribute(InstancingMode mode)
 {
 }
        /// <summary>
        /// Loads the serviceLocatorConfiguration Xml section.
        /// </summary>
        /// <param name="sectionNode">The serviceLocatorConfiguration root node.</param>
        internal void Load(XmlNode sectionNode)
        {
            try
            {
                int index = 0;
                foreach (XmlNode entryNode in sectionNode.FirstChild.ChildNodes)
                {
                    if (entryNode.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    if (entryNode.Attributes["name"] == null && entryNode.Attributes["interface"] == null)
                    {
                        ThrowException.ThrowConfigurationErrorsException(
                            string.Format("The attribute 'name' or 'interface' is required (block #{0})", index));
                    }

                    if (entryNode.Attributes["implementation"] == null)
                    {
                        ThrowException.ThrowConfigurationErrorsException(
                            string.Format("The attribute 'implementation' is required (block #{0})", index));
                    }

                    if (entryNode.Attributes["instancingMode"] == null)
                    {
                        ThrowException.ThrowConfigurationErrorsException(
                            string.Format("The attribute 'instancingMode' is required (block #{0})", index));
                    }

                    string sName      = null;
                    string sInterface = null;

                    if (entryNode.Attributes["name"] != null)
                    {
                        sName = entryNode.Attributes["name"].Value;
                        if (string.IsNullOrEmpty(sName))
                        {
                            ThrowException.ThrowConfigurationErrorsException(
                                string.Format("The attribute 'name' requires a value (block #{0})", index));
                        }
                    }

                    if (entryNode.Attributes["interface"] != null)
                    {
                        sInterface = entryNode.Attributes["interface"].Value;
                        if (string.IsNullOrEmpty(sInterface))
                        {
                            ThrowException.ThrowConfigurationErrorsException(
                                string.Format("The attribute 'interface' requires a value (block #{0})", index));
                        }
                    }

                    string sImplementation = entryNode.Attributes["implementation"].Value;
                    if (string.IsNullOrEmpty(sImplementation))
                    {
                        ThrowException.ThrowConfigurationErrorsException(
                            string.Format("The attribute 'implementation' requires a value (block #{0})", index));
                    }

                    string sInstancingMode = entryNode.Attributes["instancingMode"].Value;
                    if (string.IsNullOrEmpty(sInstancingMode))
                    {
                        ThrowException.ThrowConfigurationErrorsException(
                            string.Format("The attribute 'instancingMode' requires a value (block #{0})", index));
                    }

                    if (sInstancingMode != "Singleton" && sInstancingMode != "NewInstance")
                    {
                        ThrowException.ThrowConfigurationErrorsException(
                            string.Format("The attribute 'instancingMode' requires a value: 'Singelton' or 'NewInstance' (block #{0})", index));
                    }

                    InstancingMode instancingMode = (sInstancingMode == "Singleton") ?
                                                    InstancingMode.Singleton :
                                                    InstancingMode.NewInstance;

                    if (_instanceEntries.ContainsKey(sName ?? sImplementation))
                    {
                        ThrowException.ThrowConfigurationErrorsException(
                            string.Format("There is already a registered entry for '{0}' (block #{1})", sName ?? sImplementation, index));
                    }

                    _instanceEntries.Add(sName ?? sImplementation, new InstanceEntry
                    {
                        Name           = sName,
                        Interface      = sInterface,
                        Implementation = sImplementation,
                        InstancingMode = instancingMode
                    });

                    index++;
                }
            }
            catch (System.Exception x)
            {
                ThrowException.ThrowConfigurationErrorsException(x.Message);
            }
        }
 public NetHttpInstancingModeAttribute(InstancingMode mode)
 {
 }