Example #1
0
        /// <summary>
        /// 访问表示方法调用的节点
        /// </summary>
        /// <param name="node">方法调用节点</param>
        /// <returns></returns>
        protected Expression VisitMethodCall(BinaryExpression node)
        {
            string methodName = string.Empty;

            if (node.NodeType == ExpressionType.Modulo)
            {
                methodName = "Modulo";
            }
            else if (node.NodeType == ExpressionType.Divide)
            {
                methodName = "Divide";
            }
            else
            {
                methodName = node.Method.Name;
            }

            MemberInvokerBase invoker = this.TypeRuntime.GetInvoker("Visit" + methodName);

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} is not supported.", node.Method.DeclaringType, node.Method.Name);
            }
            else
            {
                object exp = invoker.Invoke(this, new object[] { node });
                return(exp as Expression);
            }
        }
Example #2
0
        /// <summary>
        /// 访问成员
        /// </summary>
        /// <param name="target">实例</param>
        /// <param name="memberName">成员名称</param>
        /// <param name="parameters">参数列表</param>
        /// <returns></returns>
        public object Invoke(object target, string memberName, params object[] parameters)
        {
            MemberInvokerBase invoker = null;

            this.Invokers.TryGetValue(memberName, out invoker);

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} Not Found.", _type.Name, memberName);
            }
            return(invoker.Invoke(target, parameters));
        }
Example #3
0
        /// <summary>
        /// 访问表示字段或者属性的属性的节点 a.Name.Length
        /// </summary>
        protected Expression VisitMemberMember(MemberExpression node)
        {
            MemberInvokerBase invoker = this.TypeRuntime.GetInvoker("Visit" + node.Member.Name);

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} is not supported.", node.Member.DeclaringType, node.Member.Name);
            }
            else
            {
                object exp = invoker.Invoke(this, new object[] { node });
                return(exp as Expression);
            }
        }
Example #4
0
        /// <summary>
        /// 强制释放所有资源
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            // SQLiteConnection 连续调用 Dispose 方法会抛异常
            var connection = base.Connection;

            if (connection == null)
            {
                base.Dispose(disposing);
            }
            else
            {
                bool disposed = (bool)_disposedInvoker.Invoke(connection);
                base.Dispose(!disposed);
            }
        }
Example #5
0
        /// <summary>
        /// 访问表示方法调用的节点
        /// </summary>
        /// <param name="node">方法调用节点</param>
        /// <returns></returns>
        public virtual Expression VisitMethodCall(MethodCallExpression node)
        {
            if (_typeRuntime == null)
            {
                _typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(this.GetType(), true);
            }
            MemberInvokerBase invoker = _typeRuntime.GetInvoker("Visit" + node.Method.Name);

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} is not supported.", node.Method.DeclaringType, node.Method.Name);
            }
            else
            {
                object exp = invoker.Invoke(this, new object[] { node });
                return(exp as Expression);
            }
        }
Example #6
0
        /// <summary>
        /// 访问表示方法调用的节点
        /// </summary>
        /// <param name="node">方法调用节点</param>
        /// <returns></returns>
        protected Expression VisitMethodCall(MethodCallExpression node)
        {
            MemberInvokerBase invoker = null;

            if (node.Method.Name == "Concat")
            {
                invoker = this.TypeRuntime.GetMethod("Visit" + node.Method.Name, new[] { typeof(MethodCallExpression) });
            }
            else
            {
                invoker = this.TypeRuntime.GetInvoker("Visit" + node.Method.Name);
            }

            if (invoker == null)
            {
                throw new XFrameworkException("{0}.{1} is not supported.", node.Method.DeclaringType, node.Method.Name);
            }
            else
            {
                object exp = invoker.Invoke(this, new object[] { node });
                return(exp as Expression);
            }
        }
Example #7
0
        // 反序列化导航属性
        // @prevLine 前一行数据
        // @isLine   是否同一行数据<同一父级>
        void Deserialize_Navigation(object prevModel, object model, string typeName, bool isThisLine)
        {
            // CRM_SaleOrder.Client
            // CRM_SaleOrder.Client.AccountList
            Type            type        = model.GetType();
            TypeRuntimeInfo typeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(type);

            if (string.IsNullOrEmpty(typeName))
            {
                typeName = type.Name;
            }

            foreach (var kvp in _map.Navigations)
            {
                int start      = -1;
                int end        = -1;
                var navigation = kvp.Value;
                if (navigation.FieldCount > 0)
                {
                    start = navigation.Start;
                    end   = navigation.Start + navigation.FieldCount;
                }

                string keyName = typeName + "." + navigation.Name;
                if (keyName != kvp.Key)
                {
                    continue;
                }

                var navInvoker = typeRuntime.GetInvoker(navigation.Name);
                if (navInvoker == null)
                {
                    continue;
                }

                Type navType = navInvoker.DataType;
                Func <IDataRecord, object> deserializer   = null;
                TypeRuntimeInfo            navTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navType);
                object navCollection = null;
                //if (navType.IsGenericType && navTypeRuntime.GenericTypeDefinition == typeof(List<>))
                if (TypeUtils.IsCollectionType(navType))
                {
                    // 1:n关系,导航属性为 List<T>
                    navCollection = navInvoker.Invoke(model);
                    if (navCollection == null)
                    {
                        // new 一个列表类型,如果导航属性定义为接口,则默认使用List<T>来实例化
                        TypeRuntimeInfo navTypeRuntime2 = navType.IsInterface
                            ? TypeRuntimeInfoCache.GetRuntimeInfo(typeof(List <>).MakeGenericType(navTypeRuntime.GenericArguments[0]))
                            : navTypeRuntime;
                        navCollection = navTypeRuntime2.ConstructInvoker.Invoke();
                        navInvoker.Invoke(model, navCollection);
                    }
                }

                if (!_deserializers.TryGetValue(keyName, out deserializer))
                {
                    deserializer            = _deserializerImpl.GetTypeDeserializer(navType.IsGenericType ? navTypeRuntime.GenericArguments[0] : navType, _reader, _map.PickColumns, start, end);
                    _deserializers[keyName] = deserializer;
                }

                // 如果整个导航链中某一个导航属性为空,则跳出递归
                object navModel = deserializer(_reader);
                if (navModel != null)
                {
                    if (navCollection == null)
                    {
                        // 非集合型导航属性
                        navInvoker.Invoke(model, navModel);
                        //
                        //
                        //
                    }
                    else
                    {
                        // 集合型导航属性
                        if (prevModel != null && isThisLine)
                        {
                            #region 合并列表

                            // 判断如果属于同一个主表,则合并到上一行的当前明细列表
                            // 例:CRM_SaleOrder.Client.AccountList
                            string[]          keys           = keyName.Split('.');
                            TypeRuntimeInfo   curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo <T>();
                            Type              curType        = curTypeRuntime.Type;
                            MemberInvokerBase curInvoker     = null;
                            object            curModel       = prevModel;

                            for (int i = 1; i < keys.Length; i++)
                            {
                                curInvoker = curTypeRuntime.GetInvoker(keys[i]);
                                curModel   = curInvoker.Invoke(curModel);
                                if (curModel == null)
                                {
                                    continue;
                                }

                                curType        = curModel.GetType();
                                curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(curType);

                                // <<<<<<<<<<< 一对多对多关系 >>>>>>>>>>
                                if (curType.IsGenericType && i != keys.Length - 1)
                                {
                                    curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(curType);
                                    //if (curTypeRuntime.GenericTypeDefinition == typeof(List<>))
                                    if (TypeUtils.IsCollectionType(curType))
                                    {
                                        var invoker = curTypeRuntime.GetInvoker("get_Count");
                                        int count   = Convert.ToInt32(invoker.Invoke(curModel));    // List.Count
                                        if (count > 0)
                                        {
                                            var invoker2 = curTypeRuntime.GetInvoker("get_Item");
                                            curModel       = invoker2.Invoke(curModel, count - 1);  // List[List.Count-1]
                                            curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(curModel.GetType());
                                        }
                                        else
                                        {
                                            // user.Roles.RoleFuncs=>Roles 列表有可能为空
                                            curModel = null;
                                            break;
                                        }
                                    }
                                }
                            }


                            if (curModel != null)
                            {
                                // 如果有两个以上的一对多关系的导航属性,那么在加入列表之前就需要剔除重复的实体


                                bool isAny = false;
                                if (_map.Navigations.Count > 1)
                                {
                                    if (_manyNavigationNumber == null)
                                    {
                                        _manyNavigationNumber = _map.Navigations.Count(x => IsHasMany(x.Value.Member));
                                    }
                                    if (_manyNavigationNumber != null && _manyNavigationNumber.Value > 1)
                                    {
                                        if (!_manyNavigationKeys.ContainsKey(keyName))
                                        {
                                            _manyNavigationKeys[keyName] = new HashSet <string>();
                                        }
                                        curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navModel.GetType());
                                        StringBuilder keyBuilder = new StringBuilder(64);

                                        foreach (var invoker in curTypeRuntime.KeyInvokers)
                                        {
                                            var value = invoker.Invoke(navModel);
                                            keyBuilder.AppendFormat("{0}={1};", invoker.Name, (value ?? string.Empty).ToString());
                                        }
                                        string hash = keyBuilder.ToString();
                                        if (_manyNavigationKeys[keyName].Contains(hash))
                                        {
                                            isAny = true;
                                        }
                                        else
                                        {
                                            _manyNavigationKeys[keyName].Add(hash);
                                        }
                                    }
                                }

                                if (!isAny)
                                {
                                    // 如果列表中不存在,则添加到上一行的相同导航列表中去
                                    var myAddMethod = navTypeRuntime.GetInvoker("Add");
                                    myAddMethod.Invoke(curModel, navModel);
                                }
                            }

                            #endregion
                        }
                        else
                        {
                            // 此时的 navTypeRuntime 是 List<> 类型的运行时
                            // 先添加 List 列表
                            var myAddMethod = navTypeRuntime.GetInvoker("Add");
                            myAddMethod.Invoke(navCollection, navModel);

                            var           curTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navModel.GetType());
                            StringBuilder keyBuilder     = new StringBuilder(64);

                            foreach (var invoker in curTypeRuntime.KeyInvokers)
                            {
                                var value = invoker.Invoke(navModel);
                                keyBuilder.AppendFormat("{0}={1};", invoker.Name, (value ?? string.Empty).ToString());
                            }
                            string hash = keyBuilder.ToString();
                            if (!_manyNavigationKeys.ContainsKey(keyName))
                            {
                                _manyNavigationKeys[keyName] = new HashSet <string>();
                            }
                            if (!_manyNavigationKeys[keyName].Contains(hash))
                            {
                                _manyNavigationKeys[keyName].Add(hash);
                            }
                        }
                    }

                    //if (navTypeRuntime.GenericTypeDefinition == typeof(List<>)) navTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navTypeRuntime.GenericArguments[0]);
                    if (TypeUtils.IsCollectionType(navTypeRuntime.Type))
                    {
                        navTypeRuntime = TypeRuntimeInfoCache.GetRuntimeInfo(navTypeRuntime.GenericArguments[0]);
                    }
                    if (navTypeRuntime.NavInvokers.Count > 0)
                    {
                        Deserialize_Navigation(prevModel, navModel, keyName, isThisLine);
                    }
                }
            }
        }