Ejemplo 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++)
            {
                /*typeHandlers节点信息格式
                 *                  <typeHandlers>
                 *                        <typeHandler type="string" callback="AnsiStringTypeHandler" />
                 *                  </typeHandlers>
                 */
                //store的TypeHandlers保存的是typeHandler自己点类对象
                IConfiguration handlerConfig = store.TypeHandlers[i];
                try
                {
                    //_configScope.ErrorContext.Activity = "loading typeHandler";
                    //取出handlerConfig的节点属性而已
                    TypeHandler handler = TypeHandlerDeSerializer.Deserialize(handlerConfig);

                    //configScope.ErrorContext.MoreInfo = "Check the callback attribute '" + handler.CallBackName + "' (must be a classname).";
                    //根据ITypeHandler类型的Callback字符串反射对应的类
                    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)
                    {
                        //就会将DbType类型放在Type对应的二级字典当中
                        modelStore.DataExchangeFactory.TypeHandlerFactory.Register(handler.Type, handler.DbType, typeHandler);
                    }
                    else
                    {
                        //将Type类型放入一级字典当中,其对应的字典中null对应typeHandler类对象
                        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);
                }
            }
        }
Ejemplo n.º 2
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);
                }
            }
        }