Exemplo n.º 1
0
 /// <summary>
 ///     Handles the events raised by public events of the generic class and its instances
 /// </summary>
 /// <param name="instance">The instance that raised the event</param>
 /// <param name="eventName">The name of the raised event</param>
 /// <param name="isVoid">Indicates if the raised event has a return value</param>
 /// <param name="eventArgs">An array containing the parameters of the raised event</param>
 /// <returns>Returns the result of raising registered delegates</returns>
 protected virtual object RaiseEvent(object instance, string eventName, bool isVoid, object[] eventArgs)
 {
     if (instance != null)
     {
         if (Instances.ContainsKey(instance))
         {
             var instanceId = GlobalPool.GetInstanceId(instance);
             if (instanceId != null)
             {
                 return
                     (OnPushJavascript(
                          new PushJavascriptEventArgs(
                              // ReSharper disable once UseStringInterpolation
                              string.Format("{0}.__instances[\"{1}\"].raise_{2}({3});",
                                            Identification, instanceId, eventName,
                                            string.Join(", ", eventArgs.Select(JsonConvert.SerializeObject))))
                 {
                     FireAway = isVoid
                 }));
             }
         }
         return(null);
     }
     return
         (OnPushJavascript(
              // ReSharper disable once UseStringInterpolation
              new PushJavascriptEventArgs(string.Format("{0}.raise_{1}({2});", Identification, eventName,
                                                        string.Join(", ", eventArgs.Select(JsonConvert.SerializeObject))))
     {
         FireAway = isVoid
     }));
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }