Exemplo n.º 1
0
        /// <summary>
        /// Builds the type handlers.
        /// </summary>
        /// <param name="store">The store.</param>
        private void BuildTypeHandlers(IConfigurationStore store)
        {
            for (int i = 0; i < store.TypeHandlers.Length ; i++)
            {
                IConfiguration handlerConfig = store.TypeHandlers[i];
                try
                {
                    //_configScope.ErrorContext.Activity = "loading typeHandler";
                    TypeHandler handler = TypeHandlerDeSerializer.Deserialize(handlerConfig);

                    //configScope.ErrorContext.MoreInfo = "Check the callback attribute '" + handler.CallBackName + "' (must be a classname).";
                    ITypeHandler typeHandler = null;
                    Type type = modelStore.DataExchangeFactory.TypeHandlerFactory.GetType(handler.Callback);
                    
                    object impl = Activator.CreateInstance(type);
                    if (impl is ITypeHandlerCallback)
                    {
                        typeHandler = new CustomTypeHandler((ITypeHandlerCallback)impl);
                    }
                    else if (impl is ITypeHandler)
                    {
                        typeHandler = (ITypeHandler)impl;
                    }
                    else
                    {
                        throw new ConfigurationException("The callBack type is not a valid implementation of ITypeHandler or ITypeHandlerCallback");
                    }                
                
                    //configScope.ErrorContext.MoreInfo = "Check the type attribute '" + handler.ClassName + "' (must be a class name) or the dbType '" + handler.DbType + "' (must be a DbType type name).";
                    if (handler.DbType != null && handler.DbType.Length > 0)
                    {
                        modelStore.DataExchangeFactory.TypeHandlerFactory.Register(handler.Type, handler.DbType, typeHandler);
                    }
                    else
                    {
                        modelStore.DataExchangeFactory.TypeHandlerFactory.Register(handler.Type, typeHandler);
                    }                
                
                }
                catch (Exception e)
                {
                    throw new ConfigurationException(
                        String.Format("Error registering TypeHandler class \"{0}\" for handling .Net type \"{1}\" and dbType \"{2}\". Cause: {3}",
                        handlerConfig.GetAttributeValue("callback"),
                        handlerConfig.GetAttributeValue("type"),
                        handlerConfig.GetAttributeValue("dbType"),
                        e.Message), e);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Register (add) a type handler for a type and dbType
        /// 根据前两个参数 分两次获取数据 最后把dbType加入字典当中
        /// </summary>
        /// <param name="type">the type</param>
        /// <param name="dbType">the dbType (optional, if dbType is null the handler will be used for all dbTypes)</param>
        /// <param name="handler">the handler instance</param>
        public void Register(Type type, string dbType, ITypeHandler handler)
        {
            IDictionary <string, ITypeHandler> map = null;

            typeHandlerMap.TryGetValue(type, out map);
            if (map == null)             //如果type不存在,则加入当前对象
            {
                map = new Dictionary <string, ITypeHandler>();
                typeHandlerMap.Add(type, map);
            }
            if (dbType == null)
            {
                if (logger.IsInfoEnabled)
                {
                    // notify the user that they are no longer using one of the built-in type handlers
                    ITypeHandler oldTypeHandler = null;
                    map.TryGetValue(NULL, out oldTypeHandler);

                    if (oldTypeHandler != null)
                    {
                        // the replacement will always(?) be a CustomTypeHandler
                        CustomTypeHandler customTypeHandler = handler as CustomTypeHandler;

                        string replacement = string.Empty;

                        if (customTypeHandler != null)
                        {
                            // report the underlying type
                            replacement = customTypeHandler.Callback.ToString();
                        }
                        else
                        {
                            replacement = handler.ToString();
                        }

                        // should oldTypeHandler be checked if its a CustomTypeHandler and if so report the Callback property ???
                        logger.Info("Replacing type handler [" + oldTypeHandler + "] with [" + replacement + "].");
                    }
                }

                map[NULL] = handler;//意味着dbType默认的null类型 对应当前
            }
            else
            {
                map.Add(dbType, handler);//最后加入字典当中ITypeHandler处理类对象
            }
        }