zoukankan      html  css  js  c++  java
  • 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;
            }
    
        }
    }
  • 相关阅读:
    如何避免自己上传的视频或者音频资源被下载
    定期备份服务器上的项目到本地服务器
    查看项目中的laravel的版本
    PHP高并发和大流量的解决方案
    wordpress的安装及使用
    openstack及组件简要介绍
    Java中 如何把Object类型强转成Map<String, String>类型
    JSch基本使用
    Ganymed SSH-2 for Java
    全面解析NIO
  • 原文地址:https://www.cnblogs.com/xwb2535/p/2769565.html
Copyright © 2011-2022 走看看