Using expression tree + reflection to implement simple ORM object mapping operation

Source: Internet
Author: User
Tags object model reflection
using expression tree + reflection to implement simple ORM object mapping operation

For the sake of simplicity I create a file BaseDAL.cs, the use of the method is also very simple, look at the code first to create a model

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Namespace Basedal.model
{
    [Table ("UserInfo")] public
    class Mduserinfo
    {
        ///<summary>
        ///User number
        ///</summary>
        /// 
        [PrimaryKey] public
        string Userno {set; get;}

        <summary>
        ///Store number
        ///</summary> public
        string Shopno {set; get;}

        <summary>
        ///user name
        ///</summary> public
        string USERNAMC {set; get;}
    }
then after creating a Userinfodal class inherits from Basedal
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Basedal.model;

Namespace Basedal.dal
{public
    class userinfodal:basedal<mduserinfo>
    {

    }
}
Okay, so here's how to use the example.
Using Basedal.dal;
Using Basedal.model;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;

Using System.Threading.Tasks; Namespace Basedal {class Program {static void Main (string[] args) {Userinfodal user
            Infodal = new Userinfodal (); Check list<mduserinfo> listuser = Userinfodal.query (P => p.usernamc.contains ("horse") && P.userno =

            = "00008"); change for (int i = 0; i < Listuser.count i++) {listuser[i]. USERNAMC = Listuser[i].
                USERNAMC + i;
            Userinfodal.update (Listuser[i]);
            }//Add mduserinfo UserInfo = new Mduserinfo ();
            Userinfo.userno = "00001";
            USERINFO.USERNAMC = "Test new";
            Userinfo.shopno = "10"; 

            Userinfodal.insert (UserInfo);

        Delete Userinfodal.delete (p => p.userno = = "00001"); }
    }
}

The main idea of the

is to use the expression expression tree + reflection, and is to use attribute to annotate the class, simple 400 lines of code ٩ (๑❛ᴗ❛๑) ۶ BaseDAL.cs code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Linq.Expressions;
Using System.Data.SqlClient;
Using System.Data;

Using System.Reflection; namespace Basedal {///<summary>///Data base Access class///</summary>///<typeparam name= "tentity" &
        Gt;</typeparam> public class basedal<tentity> where Tentity:class {///<summary> Database connection string///</summary> public static string SqlConnection = "Server=192.168.1.12;database=e

        atpos2;uid=sa;pwd=1234; "; 
        <summary>///Current Action table name///</summary> public string tablename {get; private set;}  <summary>///table column///</summary> private datacolumncollection Tablecolumns

        {set;get;} <summary>///Model Properties list///</summary> private list<propertyinfo> Listproper ty = new LIST&LT;PROpertyinfo> ();  <summary>///Current table primary key///</summary> private list<propertyinfo> Listprimarykey

        = new List<propertyinfo> ();

        <summary>///Callout Self-Adding column attribute///</summary> private PropertyInfo autoincrease = null;
            Public Basedal () {Type type = typeof (Tentity); #region Initialize table name object[] Objs = type.
            GetCustomAttributes (typeof (Tableattribute), false); if (OBJS.
                Length > 0) {tableattribute table = objs[0] as Tableattribute; TableName = table.
            TableName; else {tablename = type.
            Name; #endregion #region Initialize the property list propertyinfo[] Propertys = type.
            GetProperties ();
            ListProperty = new list<propertyinfo> (propertys); foreach (PropertyInfo property in ListProperty)
            {Attribute Primary = property.
               GetCustomAttribute (typeof (Primarykeyattribute));
               if (primary!=null) {Listprimarykey.add (property); Attribute Autoincrease = property.
               GetCustomAttribute (typeof (Autoincreaseattribute));
               if (autoincrease!= null) {Autoincrease = property; 
        } #endregion}///<summary>///query///</summary> <param name= "expression" ></param>///<returns></returns> public List <TEntity> Query (expression<func<tentity, bool>> Expression = null) {StringBuilder b
            Uff = new StringBuilder (); if (expression!= null) {expressionhelper.parseexpression (expression).
            body, buff); } String strSQL =String.
            Format ("select * from {0}", tablename); if (buff. Length > 0) {strSQL = "Where" +buff.
            ToString ();
            } list<tentity> listentity = new list<tentity> ();
            DataTable table = sqlhelper.executetable (strSQL); Tablecolumns = table.
            Columns; foreach (DataRow DataRow in table).
                Rows) {tentity model = datarowtoentity (DataRow);
            Listentity.add (model);
        return listentity; ///<summary>///update///</summary>///<param name= "model" ></para m>///<returns></returns> public int Update (tentity model) {#region structure
            Build Set field StringBuilder Setbuff = new StringBuilder ();
       foreach (PropertyInfo property in ListProperty) {if (listprimarykey.contains)             Continue if (Setbuff. Length > 0) {setbuff.
                Append (","); } Object value = property.
                GetValue (model); Setbuff. AppendFormat ("{0}={1}", property.)
            Name, Expressionhelper.getsqlvalue (value));
            #endregion #region build where field string Strwhere= "";
                if (Listprimarykey.count > 0) {foreach (PropertyInfo property in Listprimarykey)
                    {if (strwhere!= "") {strwhere + = "and"; } Object value = property.
                    GetValue (model); strwhere = = String. Format (' {0}={1} ', property.
                Name, Expressionhelper.getsqlvalue (value));
            } strwhere = "Where" + strwhere; #endregion string strSQL = string. Format ("Update {0} Set {1} {2} ", Tablename,setbuff.
            ToString (), strwhere);
        Return Sqlhelper.executesql (strSQL); ///<summary>///Add///</summary>///<param name= "model" ></para m>///<returns></returns> public int Insert (tentity model) {Stringbui
            Lder fieldName = new StringBuilder ();
            StringBuilder fieldvalue = new StringBuilder (); foreach (PropertyInfo listproperty) {if autoincrease!= null && property .
                Equals (autoincrease)) continue;
                    if (Fieldname.length > 0) {fieldname.append (",");
                Fieldvalue.append (","); } fieldname.append (property.
                Name); Fieldvalue.append (Expressionhelper.getsqlvalue.
            GetValue (model)); String strSQL = String. FOrmat ("Insert into {0} ({1}) Values ({2})", Tablename,fieldname.tostring (), fieldvalue.tostring ());
                if (autoincrease!= null) {strSQL + = "\r\n;select @ @IDENTITY";
                Object obj = Sqlhelper.executescalar (strSQL);
                Autoincrease.setvalue (model, obj);
            return Convert.ToInt32 (obj);
        Return Sqlhelper.executesql (strSQL); }///<summary>///delete///</summary>///<param name= "expression" >< /param>///<returns></returns> public int Delete (expression<func<tentity, BOOL&GT;&G T
            expression) {StringBuilder buff = new StringBuilder (); if (expression!= null) {expressionhelper.parseexpression (expression).
            body, buff); String strSQL = String.
            Format ("Delete from {0}", tablename); if (buff.
 Length > 0)           {strSQL = "Where" + buff.
            ToString ();
        Return Sqlhelper.executesql (strSQL); #region Private Method///<summary>///converted to model///</summary>///&LT;PA Ram Name= "DataRow" ></param>///<returns></returns> private tentity datarowtoentity (D Atarow dataRow) {ConstructorInfo construct = typeof (Tentity).
           GetConstructor (New type[]{}); Object model = construct.
           Invoke (NULL);
               for (int i = 0; i < Listproperty.count i++) {PropertyInfo property = Listproperty[i]; If (property, Tablecolumns.contains. Name)) {property. SetValue (model, Datarow[property.
               Name], NULL);
        } return model as tentity; #endregion} #region expressionhelper + expression help class///<summary>///expression HelpClass///</summary> class Expressionhelper {///<summary>///parsing where condition/// </summary>///<param name= "expression" ></param>///<param name= "Buff" ></param > public static void ParseExpression (Expression Expression, StringBuilder buff) {Binaryexp
            Ression binary = null;
            Memberexpression left = null;
            ConstantExpression rigth = null; if (expression. NodeType = = expressiontype.andalso) {parseexpression (expression as system.linq.expressions.b inaryexpression).
                Left, buff); Buff.
                Append ("and"); ParseExpression ((expression as System.Linq.Expressions.BinaryExpression).
            right, buff); else if (expression. NodeType = = Expressiontype.orelse) {parseexpression (expression as System.Linq.Expressions.Bi naryexpression).
       Left, buff);         Buff.
                Append ("Or"); ParseExpression ((expression as System.Linq.Expressions.BinaryExpression).
            right, buff); else if (expression. NodeType = = Expressiontype.call) {Methodcallexpression call = expression as Methodcallexpress
                Ion String methodname = call.
                Method.name; if (methodname = = "Contains") {left = call.
                    Object as Memberexpression; Rigth = call.
                    Arguments[0] as ConstantExpression; Buff. AppendFormat ("{0} like '%{1}% '", left. Member.name, Rigth.
                Value); } else {binary = expression as System.Linq.Expressions.BinaryExpres
                Sion left = binary.
                Left as Memberexpression; rigth = binary.
                Right as ConstantExpression; switch (expression. NodeType) {Case ExpressiOnType.Equal:buff. AppendFormat (' {0} = {1} ', left. Member.name, Getsqlvalue (rigth.
                        Value));
                    Break Case ExpressionType.NotEqual:buff. AppendFormat ("{0} <> {1}", left. Member.name, Getsqlvalue (rigth.
                        Value));
                    Break Case expressiontype.greaterthan://is larger than buff. AppendFormat ("{0} > {1}", left. Member.name, Getsqlvalue (rigth.
                        Value));
                    Break Case expressiontype.greaterthanorequal://is greater than or equal to buff. AppendFormat ("{0} >= {1}", left.) Member.name, Getsqlvalue (rigth.
                        Value));
                    Break Case expressiontype.lessthan://is less than buff. AppendFormat ("{0} < {1}", left. Member.name, Getsqlvalue (rigth.
                        Value));
                    Break
                    Case expressiontype.lessthanorequal://less than or equal    Buff. AppendFormat ("{0} <= {1}", left.) Member.name, Getsqlvalue (rigth.
                        Value));

                Break
        } return; ///<summary>///Fetch SQL parameter value///</summary>///<param name= "obj" ></p
            aram>///<returns></returns> public static string Getsqlvalue (Object obj) {
            if (obj = null) return ""; if (obj. GetType (). Equals (typeof (String)) {return string. Format ("' {0} '", obj.)
            ToString ()); else if (obj. GetType (). Equals (typeof (DateTime)) {return string. Format ("' {0} '"), Convert.todatetime (obj).
            ToString ("Yyyy-mm-dd HH:mm:ss")); else if (obj. GetType (). Equals (typeof (int)) | | Obj. GetType (). Equals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.