コード例 #1
0
        /// <summary>
        ///     Determines whether the specified instance of the specified type is configured.
        /// </summary>
        /// <param name="type">The Type of the instance to check.</param>
        /// <param name="instanceName">The name of the instance to check.</param>
        /// <returns>A Result containing the result of the operation and a boolean containing the outcome of the lookup.</returns>
        public IResult <bool> IsInstanceConfigured(Type type, string instanceName)
        {
            logger.EnterMethod(xLogger.Params(type, new xLogger.ExcludedParam(), instanceName));
            Result <bool> retVal = new Result <bool>();

            // check to see if the type is in the comfiguration
            if (InstanceDictionary.ContainsKey(type.FullName))
            {
                // check to see if the specified instance is in the type configuration
                if (!InstanceDictionary[type.FullName].ContainsKey(instanceName))
                {
                    retVal.AddError("The specified instance name '" + instanceName + "' wasn't found in the configuration for type '" + type.Name + "'.");
                }
            }
            else
            {
                retVal.AddError("The specified type '" + type.Name + "' was not found in the configuration.");
            }

            // if any messages were generated the configuration wasn't found, so return false.
            retVal.ReturnValue = retVal.ResultCode != ResultCode.Failure;

            logger.ExitMethod(retVal);
            return(retVal);
        }
コード例 #2
0
        /// <summary>
        ///     Adds the specified configuration to the specified instance of the specified type.
        /// </summary>
        /// <typeparam name="T">The configuration Type.</typeparam>
        /// <param name="type">The Type of the instance to be configured.</param>
        /// <param name="instanceConfiguration">The ApplicationConfiguration instance to which to add the new configuration.</param>
        /// <param name="instanceName">The name of the instance to configure.</param>
        /// <returns>A Result containing the result of the operation.</returns>
        public IResult <T> AddInstance <T>(Type type, object instanceConfiguration, string instanceName)
        {
            logger.EnterMethod(xLogger.Params(type, instanceConfiguration, new xLogger.ExcludedParam(), instanceName));

            Result <T> retVal = new Result <T>();

            if (!TypeRegistry.IsRegistered(type))
            {
                retVal.AddError("The type '" + type.Name + "' is configurable but has not been registered.");
            }
            else if (!IsInstanceConfigured(type, instanceName).ReturnValue)
            {
                // ensure the configuration doesn't already exist
                logger.Trace("Inserting configuration into the Configuration dictionary...");

                // if the configuration doesn't contain a section for the type, add it
                if (!InstanceDictionary.ContainsKey(type.FullName))
                {
                    InstanceDictionary.Add(type.FullName, new Dictionary <string, object>());
                }

                // add the default configuration for the requested type/instance to the configuration.
                InstanceDictionary[type.FullName].Add(instanceName, instanceConfiguration);

                retVal.ReturnValue = (T)instanceConfiguration;

                logger.Trace("The configuration was inserted successfully.");
            }
            else
            {
                retVal.AddError("The configuration for instance '" + instanceName + "' of type '" + type.Name + "' already exists.");
            }

            retVal.LogResult(logger.Debug);
            logger.ExitMethod(retVal);
            return(retVal);
        }