Example #1
0
        /// <summary>
        /// 从DataTable获取一个实体列表
        /// </summary>
        /// <param name="table">DataTable实例</param>
        /// <remarks>
        /// <list type="bullet">
        /// <item><description>建议不要直接从DataTable返回实体,而是通过CPQuery或者StoreProcedure返回实体</description></item>
        /// </list>
        /// </remarks>
        /// <example>
        ///		<para>下面的代码演示了从DataTable获取一个实体列表的用法</para>
        ///		<code>
        ///		<![CDATA[
        ///		//存储过程中包含两个SELECT语句,返回两个结果集
        ///		DataSet ds = StoreProcedure.Create("usp_GetTestDataType").FillDataSet();
        ///
        ///		foreach( DataTable table in ds2.Tables ) {
        ///
        ///			//将DataTable转换为实体集合
        ///			List<TestDataType> list = table.ToList<TestDataType>();
        ///
        ///		}
        ///		]]>
        ///		</code>
        /// </example>
        /// <typeparam name="T">实体类型</typeparam>
        /// <exception cref="ArgumentNullException">table参数为null</exception>
        /// <returns>实体列表</returns>
        public static List <T> ToList <T>(this DataTable table) where T : class, new()
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            Type type = typeof(T);

            TypeDescription description = TypeDescriptionCache.GetTypeDiscription(type);

            if (description.ExecuteFunc != null)
            {
                try
                {
                    return(description.ExecuteFunc(10, new object[] { table }) as List <T>);
                }
                catch (System.Exception ex)
                {
                    //这里不希望调用者看到代码生成器产生的代码结构,于是在这里抛出捕获到的异常
                    throw ex;
                }
            }
            else if (type.IsSubclassOf(typeof(BaseEntity)))
            {
                throw new InvalidProgramException(
                          string.Format("类型 {0} 找不到ToList的操作方法,请确认已将实体类型定义在*.Entity.dll结尾的程序集中,且不是嵌套类,并已提供无参的构造函数。", type.FullName));
            }
            else
            {
                return(DbHelper.ToList <T>(table, description));
            }
        }
Example #2
0
        internal static T ToSingle <T>(OracleCommand cmd, string connectionString) where T : class, new()
        {
            Type type = typeof(T);

            TypeDescription description = TypeDescriptionCache.GetTypeDiscription(type);

            using (ConnectionScope scope = new ConnectionScope(connectionString))
            {
                return(scope.Current.ExecuteCommand <T>(cmd, p => {
                    using (OracleDataReader reader = p.ExecuteReader())
                    {
                        if (description.ExecuteFunc != null)
                        {
                            return description.ExecuteFunc(2, new object[] { reader }) as T;
                        }
                        else if (type.IsSubclassOf(typeof(BaseEntity)))
                        {
                            throw BaseEntity.GetNonStandardExecption(type);
                        }
                        else
                        {
                            return ToSingle <T>(reader, description);
                        }
                    }
                }));
            }
        }
Example #3
0
        /// <summary>
        /// 此API不宜在项目代码中调用,仅供内部使用。
        /// </summary>
        public void TrackChange()
        {
            TypeDescription description = TypeDescriptionCache.GetTypeDiscription(GetType());

            if (description.ExecuteFunc == null)
            {
                throw GetNonStandardExecption(this.GetType());
            }

            bakObject = description.ExecuteFunc(13, new object[] { this }) as BaseEntity;
        }
Example #4
0
        /// <summary>
        /// 获取查询SQL
        /// </summary>
        /// <returns>查询SQL</returns>
        public virtual string GetSelectSQL()
        {
            TypeDescription description = TypeDescriptionCache.GetTypeDiscription(GetType());

            if (description.ExecuteFunc == null)
            {
                throw GetNonStandardExecption(this.GetType());
            }
            try
            {
                return(description.ExecuteFunc(15, null) as string);
            }
            catch (System.Exception ex)
            {
                //这里不希望调用者看到代码生成器产生的代码结构,于是在这里抛出捕获到的异常
                throw ex;
            }
        }
Example #5
0
        internal CPQuery GetCPQuery(int flag, params object[] parameters)
        {
            TypeDescription description = TypeDescriptionCache.GetTypeDiscription(GetType());

            if (description.ExecuteFunc == null)
            {
                throw GetNonStandardExecption(this.GetType());
            }

            try
            {
                return(description.ExecuteFunc(flag, parameters) as CPQuery);
            }
            catch (System.Exception ex)
            {
                //这里不希望调用者看到代码生成器产生的代码结构,于是在这里抛出捕获到的异常
                throw ex;
            }
        }
Example #6
0
        /// <summary>
        /// 将泛类型集合List类转换成DataTable
        /// </summary>
        /// <param name="list">泛类型集合</param>
        /// <returns></returns>
        public static DataTable ToDataTable <T>(this List <T> entitys) where T : class, new()
        {
            //检查实体集合不能为空
            if (entitys == null)
            {
                throw new ArgumentNullException("需转换的集合为空");
            }
            //取出第一个实体的所有Propertie
            Type            entityType          = typeof(T);
            TypeDescription description         = TypeDescriptionCache.GetTypeDiscription(entityType);
            Dictionary <string, DbMapInfo> dict = description.MemberDict;


            //生成DataTable的structure
            //生产代码中,应将生成的DataTable结构Cache起来,此处略
            DataTable dt = new DataTable();

            foreach (string key in dict.Keys)
            {
                Type colType = dict[key].PropertyInfo.PropertyType;
                if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable <>)))
                {
                    colType = colType.GetGenericArguments()[0];
                }
                dt.Columns.Add(key, colType);
            }
            //将所有entity添加到DataTable中
            foreach (T entity in entitys)
            {
                object[] entityValues = new object[dict.Keys.Count];


                int i = 0;
                foreach (string key in dict.Keys)
                {
                    entityValues[i] = dict[key].PropertyInfo.FastGetValue(entity);
                    i++;
                }
                dt.Rows.Add(entityValues);
            }
            return(dt);
        }
Example #7
0
        //private string GeneratorCode(TypesAndReferences threadParam)
        //{
        //    //测试ERP258系统,1253个实体类,5线程编译,每线程的StringBuilder长度数据如下:
        //    //3826393,4451978,3653788,4433206,4545317
        //    //根据以上数字,初始值设置为1024*1024*5
        //    StringBuilder sb = new StringBuilder(1024 * 1024 * 5);

        //    // 生成命名空间和命名空间的引用
        //    sb.AppendLine(CodeGenerator.GetCodeHeader());

        //    // 生成一个工具类
        //    sb.AppendLine(CodeGenerator.GetCodeUtil());

        //    // 为每个数据实体类型生成数据访问代码
        //    foreach (Type type in threadParam.EntityTypes)
        //    {
        //        CodeGenerator generator = new CodeGenerator(type);

        //        // 数据访问代码的类型名称根据数据实体名称得到,具体生成方法请参考下面这行代码。
        //        string className = "CodeDom_" + type.FullName.Replace(".", "_");

        //        // 生成数据访问代码。
        //        string code = generator.GetCode(className);
        //        sb.AppendLine(code);
        //    }

        //    sb.Append("}");

        //    return sb.ToString();
        //}


        //private Assembly CompilerCode(string code, string[] referenceAssemblies)
        //{
        //    CompilerParameters cp = new CompilerParameters();
        //    cp.GenerateExecutable = false;
        //    cp.GenerateInMemory = true;

        //    for (int i = 0; i < referenceAssemblies.Length; i++)
        //        cp.ReferencedAssemblies.Add(referenceAssemblies[i]);


        //    using (CSharpCodeProvider csProvider = new CSharpCodeProvider())
        //    {

        //        // 编译代码
        //        CompilerResults cr = csProvider.CompileAssemblyFromSource(cp, code);

        //        // 检查是否发生编译错误。
        //        if (cr.Errors != null && cr.Errors.HasErrors)
        //        {
        //            throw new CompileException { Code = code, CompilerResult = cr };
        //        }

        //        return cr.CompiledAssembly;
        //    }
        //}

        private void CreateDelegate(List <Type> types, Assembly assembly)
        {
            foreach (Type type in types)
            {
                // 从编译结果中查找类型名称。
                string className = "_Tool.AutoGenerateCode.CodeDom_" + type.FullName.Replace(".", "_");
                // 从编译结果中查找类型
                Type targetType = assembly.GetType(className);

                // 从找到的类型中查找供外面调用的接口方法
                MethodInfo methodExecute = targetType.GetMethod("Execute", BindingFlags.Static | BindingFlags.Public);
                // 生成调用委托
                Func <int, object[], object> executeFunc = Delegate.CreateDelegate(typeof(Func <int, object[], object>), methodExecute) as Func <int, object[], object>;

                // 保存委托。
                TypeDescription description = new TypeDescription()
                {
                    ExecuteFunc = executeFunc
                };
                TypeDescriptionCache.SaveComplieResult(type, description);
            }
        }