Exemple #1
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);
        }
 /// <summary>
 ///     Registers an instance of the generic type and adds it to the list of registered instances
 /// </summary>
 /// <param name="instance">The instance of the generic type to ad</param>
 /// <param name="variableName">The name of the variable that is accessible from the Javascript side</param>
 /// <returns>Returns <see langword="this" /> instance to be used for other operations</returns>
 public virtual ClassBridge <T> AddInstance(T instance, string variableName)
 {
     if (instance != null)
     {
         if (!Instances.ContainsKey(instance))
         {
             Instances.Add(instance, new List <string>());
             if (!InstancesEventsDelegates.ContainsKey(instance))
             {
                 InstancesEventsDelegates.Add(instance, new Dictionary <EventInfo, Delegate>());
             }
             foreach (
                 var info in
                 Events.Where(info => !info.GetAddMethod().IsStatic&& !info.GetRemoveMethod().IsStatic))
             {
                 var eventDelegate =
                     InstancesEventsDelegates[instance].FirstOrDefault(pair => pair.Key == info).Value;
                 if (eventDelegate == null)
                 {
                     var eventInvokerReturn = info.EventHandlerType.GetMethod("Invoke").ReturnType;
                     if ((eventInvokerReturn == typeof(void)) || (eventInvokerReturn == typeof(object)))
                     {
                         eventDelegate = ClassBridge.CreateProxyDelegateForEvent(info, instance, RaiseEvent);
                     }
                     else
                     {
                         eventDelegate = ClassBridge.CreateProxyDelegateForEvent(info, instance,
                                                                                 (o, s, arg3, arg4) =>
                                                                                 ClassBridge.NormalizeVariable(RaiseEvent(o, s, arg3, arg4), eventInvokerReturn,
                                                                                                               false));
                     }
                     InstancesEventsDelegates[instance].Add(info, eventDelegate);
                 }
                 info.AddEventHandler(instance, eventDelegate);
             }
             OnPushJavascript(
                 new FireJavascriptEventArgs(ClassBridge.GenerateInstanceChange(Identification,
                                                                                GlobalPool.GetInstanceId(instance), false)));
         }
     }
     if (!string.IsNullOrWhiteSpace(variableName))
     {
         foreach (
             var key in
             Instances.Keys.Where(key => key != instance)
             .Where(key => Instances[key].Contains(variableName))
             .ToArray())
         {
             RemoveInstance(variableName, key as T);
         }
         if (instance != null)
         {
             if (!Instances[instance].Contains(variableName))
             {
                 Instances[instance].Add(variableName);
                 OnPushJavascript(
                     new FireJavascriptEventArgs(ClassBridge.GenerateInstanceVariable(Identification,
                                                                                      GlobalPool.GetInstanceId(instance), variableName)));
             }
         }
         else
         {
             OnPushJavascript(
                 new FireJavascriptEventArgs(ClassBridge.GenerateInstanceVariable(Identification, null,
                                                                                  variableName)));
         }
     }
     return(this);
 }