う三余无梦生

C# 自定义ORM 以及 通过委托实现事务(第一版)

本demo是个人偶尔头脑发热,想要实现的一个ORM,用于在多层结构下,实现数据库的操作,包括多表事务操作。

具体请查看源码。下载源码

结构图片: 

包括 数据持久结构,业务处理结构,界面结构。

业务实体类与数据模型的区别在于,数据模型不存在依赖的模型,但是业务实体类中存在依赖的实体。比如:DB.Entity中的Student学生类只有ClassID ,但是Ligic.Entity中的Student类却拥有Class 作为一个属性。

数据库事务操作委托类:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DB.Util;

namespace DB.BIZ
{
    class DBDelegate<T>
    {
        public delegate T OpenTranscation(TsqlCommand tsql, SqlConnection conn, SqlTransaction tran);


        public T Open(string strConn, TsqlCommand tsql, OpenTranscation action)
        {
            T t = default(T);
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();//开启一个事物

                try
                {

                    t = action(tsql, conn, tran);

                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                    conn.Close();
                }
            }
            return t;
        }

    }
}

DB.DAO中的持久方法,仅返回操作TSQL以及查询参数。DB.BIZ中通过事务,进行持久化。

数据持久业务类,是真正的与数据库的操作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DB.Entity;
using DB.Util;
using DB.ORM;




namespace DB.DAO
{

    public class StudentDAO : IEntity<Student>
    {

        public TsqlCommand Insert(Student entity)
        {
            TsqlCommand sql = ModelReflection<Student>.Insert(entity);
            return sql;
        }

        public TsqlCommand Update(Student entity)
        {
            TsqlCommand sql = ModelReflection<Student>.Update(entity);
            return sql;
        }

        public TsqlCommand Delete(object ID)
        {
            TsqlCommand sql = ModelReflection<Student>.Delete(ID);
            return sql;
        }

        public TsqlCommand GetByID(object ID)
        {
            TsqlCommand sql = ModelReflection<Student>.Select();

            TsqlParameter pa = new TsqlParameter()
            {
                PiName = "ID",
                Value = ID,
                Condition = CompareCondition.EqualTo
            };
            ModelReflection<Student>.CreateParameter(sql, pa);

            return sql;
        }

        public TsqlCommand GetList()
        {
            TsqlCommand sql = ModelReflection<Student>.Select();
            return sql;
        }

        public TsqlCommand DeleteByIds(string[] ids)
        {
            string strIds = string.Join(",", ids);
            TsqlCommand sql = ModelReflection<Student>.DeleteByIds(strIds);
            return sql;
        }


        public TsqlCommand GetListByPaging(PagingCondtion<Student> condition)
        {
            return null;
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;

using DB.Util;
using DB.Entity;
using Logic.Entity;
using DB.DAO;


namespace DB.BIZ
{
    public class StudentBIZ
    {

        DBSchool db = new DBSchool();
        StudentDAO dal = new StudentDAO();

        public LeStudent GetStudenById(int id)
        {

            LeStudent student = null;
            using (SqlConnection conn = new SqlConnection(db.GetConnection()))
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();//开启一个事物

                try
                {

                    TsqlCommand tsql = dal.GetByID(id);
                    SqlCommand command = tsql.CreateCommand(conn, tran);
                    Student stu = SqlModelHelper<Student>.GetSingleObjectBySql(command);

                    student = new LeStudent();
                    student.ID = stu.ID;
                    student.Name = stu.Name;


                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                    conn.Close();
                }
            }
            return student;
        }



        public bool DeleteStudentById(int id)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(db.GetConnection()))
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();//开启一个事物

                try
                {
                    TsqlCommand tsql = dal.Delete(id);
                    SqlCommand command = tsql.CreateCommand(conn, tran);
                    int rowCount = command.ExecuteNonQuery();
                    result = (rowCount > 0);
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
                finally
                {
                    tran.Dispose();
                    conn.Close();
                }
            }
            return result;
        }



        public bool DeleteStudentById2(int id)
        {
            bool result = false;
            DBDelegate<int> dele = new DBDelegate<int>();
            int rowCount = dele.Open(db.GetConnection(), dal.Delete(id),Delete);

            result = (rowCount > 0);
            return result;
        }

        int Delete(TsqlCommand tsql, SqlConnection conn, SqlTransaction tran)
        {
            SqlCommand command = tsql.CreateCommand(conn, tran);
            int rowCount = command.ExecuteNonQuery();
            return rowCount;
        }

        public LeStudent GetStudenById2(int id)
        {
            LeStudent student = null;
            DBDelegate<Student> dele = new DBDelegate<Student>();

            Student stu = dele.Open(db.GetConnection(), dal.GetByID(id), GetModel);

            student = new LeStudent();
            student.ID = stu.ID;
            student.Name = stu.Name;
            return student;
        }

        Student GetModel(TsqlCommand tsql, SqlConnection conn, SqlTransaction tran)
        {
            SqlCommand command = tsql.CreateCommand(conn, tran);
            Student stu = SqlModelHelper<Student>.GetSingleObjectBySql(command);
            return stu;
        }

    }
}

 

 

 

 

 

 

 

 

 

 

 

posted on 2012-11-14 11:12  う三余无梦生  阅读(548)  评论(0编辑  收藏  举报

导航