日期:2014-05-20  浏览次数:20939 次

不使用反射的实体类,大家评评?
看过很多ORM的实体类方案,大多是用反射来读数据库数据,这样当频繁操作实体类的时候效率很低,我借鉴了一位朋友的思路,采用.NET 2.0的泛型技术,为实体类提供一个通用的查询和持久化方案。
先看看实体类定义:
 
C# code

using PWMIS.SqlMapper.Entity;


namespace PLZDModel
{
    public class PFT_Report : Entity
    {
        public PFT_Report()
        {
            TableName = "PFT_Report";
            //IdentityName = "标识列";//如果指定了标识列,可以处理自增列插入问题
            //PrimaryKeys.Add("主键列");//指定主键列方可以删除和更新实体数据


            AddProperty("ID", default(System.Int32));
            AddProperty("FinancialPlanersNo", default(System.String));
            AddProperty("FundAccount", default(System.String));
            AddProperty("CityCode", default(System.String));
            AddProperty("BankCode", default(System.String));
            AddProperty("NetWork", default(System.String));
            AddProperty("ApplyTime", default(System.DateTime));
            AddProperty("FileName", default(System.String));
            AddProperty("GenerateTime", default(System.DateTime));
        }


        public System.Int32 ID
        {
            get { return (System.Int32)getProperty("ID"); }
            set { setProperty("ID", value); }
        }

        public System.String FinancialPlanersNo
        {
            get { return (System.String)getProperty("FinancialPlanersNo"); }
            set { setProperty("FinancialPlanersNo", value); }
        }

        public System.String FundAccount
        {
            get { return (System.String)getProperty("FundAccount"); }
            set { setProperty("FundAccount", value); }
        }

        public System.String CityCode
        {
            get { return (System.String)getProperty("CityCode"); }
            set { setProperty("CityCode", value); }
        }

        public System.String BankCode
        {
            get { return (System.String)getProperty("BankCode"); }
            set { setProperty("BankCode", value); }
        }

        public System.String NetWork
        {
            get { return (System.String)getProperty("NetWork"); }
            set { setProperty("NetWork", value); }
        }

        public System.DateTime ApplyTime
        {
            get { return (System.DateTime)getProperty("ApplyTime"); }
            set { setProperty("ApplyTime", value); }
        }

        public System.String FileName
        {
            get { return (System.String)getProperty("FileName"); }
            set { setProperty("FileName", value); }
        }

        public System.DateTime GenerateTime
        {
            get { return (System.DateTime)getProperty("GenerateTime"); }
            set { setProperty("GenerateTime", value); }
        }


    }
}



之后,便可以这样使用实体类:
1,查询:
C# code

class Program
    {
        static void Main(string[] args)
        {
            PFT_Report utlReport = new PFT_Report();
            OQL oqlQuerry = new OQL(utlReport);
            utlReport.FundAccount = "1234234242423";
            oqlQuerry.TopCount = 3;
            
            oqlQuerry.Select(utlReport.ApplyTime , utlReport.BankCode,utlReport.FileName ,utlReport.FundAccount )
                .Where(utlReport.FundAccount)
                .OrderBy(utlReport.GenerateTime, "desc");
            Console.WriteLine ("SQL="+oqlQuerry.ToString ());

            Console.Read();

        }


运行程序将输出:
SQL=SELECT Top 3 ApplyTime,BankCode,FileName,FundAccount
 FROM PFT_Report
  Where FundAccount=@FundAccount
  Order by FundAccount desc