Пример #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);
                }
            }
        }
        public static void Deserialize(XmlNode node, ConfigurationScope configScope)
        {
            TypeHandler         handler    = new TypeHandler();
            NameValueCollection attributes = NodeUtils.ParseAttributes(node, configScope.Properties);

            handler.CallBackName = NodeUtils.GetStringAttribute(attributes, "callback");
            handler.ClassName    = NodeUtils.GetStringAttribute(attributes, "type");
            handler.DbType       = NodeUtils.GetStringAttribute(attributes, "dbType");
            handler.Initialize();
            configScope.ErrorContext.MoreInfo = "Check the callback attribute '" + handler.CallBackName + "' (must be a classname).";
            ITypeHandler handler2 = null;
            object       obj2     = Activator.CreateInstance(configScope.SqlMapper.TypeHandlerFactory.GetType(handler.CallBackName));

            if (obj2 is ITypeHandlerCallback)
            {
                handler2 = new CustomTypeHandler((ITypeHandlerCallback)obj2);
            }
            else
            {
                if (!(obj2 is ITypeHandler))
                {
                    throw new ConfigurationException("The callBack type is not a valid implementation of ITypeHandler or ITypeHandlerCallback");
                }
                handler2 = (ITypeHandler)obj2;
            }
            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))
            {
                configScope.DataExchangeFactory.TypeHandlerFactory.Register(TypeUtils.ResolveType(handler.ClassName), handler.DbType, handler2);
            }
            else
            {
                configScope.DataExchangeFactory.TypeHandlerFactory.Register(TypeUtils.ResolveType(handler.ClassName), handler2);
            }
        }
Пример #3
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);
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Initialize the argument property.
        /// </summary>
        /// <param name="constructorInfo"></param>
        /// <param name="configScope"></param>
        public void Initialize(ConfigurationScope configScope, ConstructorInfo constructorInfo)
        {
            // Search argument by his name to set his type
            ParameterInfo[] parameters = constructorInfo.GetParameters();

            bool found = false;

            for (int i = 0; i < parameters.Length; i++)
            {
                found = (parameters[i].Name == _argumentName);
                if (found)
                {
                    _argumentType = parameters[i].ParameterType;
                    break;
                }
            }
            if (!string.IsNullOrEmpty(CallBackName))
            {
                configScope.ErrorContext.MoreInfo = "Argument property (" + _argumentName + "), check the typeHandler attribute '" + CallBackName + "' (must be a ITypeHandlerCallback implementation).";
                try
                {
                    Type type = configScope.SqlMapper.TypeHandlerFactory.GetType(CallBackName);
                    ITypeHandlerCallback typeHandlerCallback = (ITypeHandlerCallback)Activator.CreateInstance(type);
                    TypeHandler = new CustomTypeHandler(typeHandlerCallback);
                }
                catch (Exception e)
                {
                    throw new ConfigurationException("Error occurred during custom type handler configuration.  Cause: " + e.Message, e);
                }
            }
            else
            {
                configScope.ErrorContext.MoreInfo = "Argument property (" + _argumentName + ") set the typeHandler attribute.";
                TypeHandler = ResolveTypeHandler(configScope, _argumentType, CLRType, DbType);
            }
        }