Exemple #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));
            }
        }
Exemple #2
0
        internal static T ToSingle <T>(SqlCommand cmd) where T : class, new()
        {
            Type type = typeof(T);

            TypeDescription description = TypeDescriptionCache.GetTypeDiscription(type);

            using (ConnectionScope scope = new ConnectionScope()) {
                return(scope.Current.ExecuteCommand <T>(cmd, p => {
                    using (SqlDataReader 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);
                        }
                    }
                }));
            }
        }
Exemple #3
0
        /// <summary>
        /// 实体校验
        /// </summary>
        /// <param name="fields">忽略字段</param>
        /// <returns></returns>
        public Boolean Valid(List <String> fields)
        {
            if (fields == null)
            {
                fields = new List <String>();
            }
            TypeDescription description = TypeDescriptionCache.GetTypeDiscription(GetType());

            return((Boolean)description.ExecuteFunc(14, new object[] { this, fields }));
        }
        /// <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;
        }
        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;
            }
        }
        /// <summary>
        /// 将Map平台AppForm格式xml字符串转换为实体对象集合
        /// </summary>
        /// <remarks>
        /// <list type="bullet">
        /// <item><description>xml结构为3层,第1层被忽略,第2层为数据库表名,第3层为字段名</description></item>
        /// <item><description>xml结构第2层节点必须包含keyname,keyvalue属性,keyvalue属性可以为空</description></item>
        /// <item><description>如果类,属性存在Alias别名标记,则按照别名赋值,否则按照类名,属性名赋值</description></item>
        /// </list>
        /// </remarks>
        /// <example>
        /// <para>下面的代码演示了从ConvertXmlToList()方法的用法</para>
        /// <code>
        /// <![CDATA[
        /// using System;
        /// using System.Collections.Generic;
        /// using System.Linq;
        /// using System.Text;
        ///
        /// //引入命名空间
        /// using Mysoft.Map.Extensions;
        /// using Mysoft.Map.Extensions.Xml;
        /// using Mysoft.Map.Extensions.DAL;
        /// namespace Demo
        /// {
        ///     public class DemoBusiness
        ///     {
        ///			public void Demo(){
        ///				string xml = @"<UserData>
        ///							     <cb_Contract keyname="ContractGUID" keyvalue="">
        ///								   <ContractName>测试合同一</ContractName>
        ///								   <ContractCode>HT-001</ContractCode>
        ///								   ...其他值
        ///							     </cb_Contract>
        ///							     <cb_Contract keyname="ContractGUID" keyvalue="">
        ///								   <ContractName>测试合同二</ContractName>
        ///								   <ContractCode>HT-002</ContractCode>
        ///								   ...其他值
        ///							     </cb_Contract>
        ///							   </UserData>";
        ///
        ///				List<CbContract> contracts = XmlDataEntity.ConvertXmlToList<CbContract>(xml);
        ///
        ///				foreach(CbContract contract in contracts){
        ///					//主键为空,表示是新增
        ///					if (contract.ContractGUID == Guid.Empty){
        ///						contract.ContractGUID = Guid.NewGuid();
        ///						//插入到数据库
        ///						contract.Insert();
        ///					}
        ///					else{
        ///						//更新到数据库
        ///						contract.Update();
        ///					}
        ///				}
        ///			}
        ///     }
        /// }
        /// ]]>
        /// </code>
        /// </example>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="xml">xml字符串</param>
        /// <returns>实体对象集合</returns>
        public static List <T> ConvertXmlToList <T>(string xml) where T : BaseEntity, new()
        {
            if (string.IsNullOrEmpty(xml))
            {
                throw new ArgumentNullException("xml");
            }

            Type            type        = typeof(T);
            TypeDescription description = TypeDescriptionCache.GetTypeDiscription(type);

            if (description.ExecuteFunc == null)
            {
                throw BaseEntity.GetNonStandardExecption(type);
            }

            try {
                return(description.ExecuteFunc(12, new object[] { xml }) as List <T>);
            }
            catch (System.Exception ex) {
                //这里不希望调用者看到代码生成器产生的代码结构,于是在这里抛出捕获到的异常
                throw ex;
            }
        }