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; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述