首页 > 学院 > 开发设计 > 正文

分享一个自己写的.Net的ORM工具

2019-11-17 03:03:02
字体:
来源:转载
供稿:网友

分享一个自己写的.Net的ORM工具

  注册博客园帐号也有好几年了,之前注册帐号主要是为了看别人的文章下载东西的时候方便。从来没有写过什么博客,一直以为只要注册了帐号就可以写博客,最近用到了才发现还得申请一下,于是就申请了博客,算了也不扯这么多没用的了,直接进入主题吧!

  

  

  网上开源的ORM工具也不少,开源中国上就有不少,很多都下载试用过,不过感觉用起来都不是很方便,园子里面也有不少人分享自己写的ORM工具,用过一个叫 MySoft.Data 的ORM工具,感觉里面的链式调用函数的写法不错,但是用起来感觉也有些不如意的地方,于是某个周末的晚上自己研究人家的代码就想着自己来实现一个ORM工具,又扯远了...

  自己弄得这个ORM工具也不知道起什么名字好,于是就直接就叫 ORM,写这个的时候参考过 MySoft.Data、 NBearLite、PetaPoco ,整个设计的感觉不算太好,反正就是凭着自己的感觉来写的,接口用的比较少,抽象类用到的比较多,支持多数据库,但是只在SqlServer和SQLite中测试过,其他数据库还没有测试,可以实现较复杂的查询,增删改、事务等操作,ORM没有经过系统的测试,也就是自己写些小例子测试一下,所以稳定性、性能方面不敢保证,建议大家先不要用到项目中去,大家可以下载代码自己改改、测试测试然后再在项目中使用。

  下面说下用法吧,ORM 中所有类都用的一个命名空间就叫 ORM,只是在代码中对不能作用的类做了些分类放在不同文件夹,但都是一个命名空间。

  

  

  首先要创建一个 IDbsession 对象:

  

  

<!--数据库连接字符串如下--><add name="connStr" connectionString="Data Source=.;Initial Catalog=dbName;Persist Security Info=True;User ID=sa;PassWord=******" PRoviderName="System.Data.SqlClient"/>

IDbSession db = DbSession.New('connStr');

  生成实体类:

namespace ORM.Demo.Model {    using System;    using ORM;        public partial class SysUser2Role : TableEntity {        private Guid? _SysUser2RoleOID;        /// <summary>        /// SysUser2RoleOID        /// </summary>        public Guid? SysUser2RoleOID        {            get { return _SysUser2RoleOID; }            set { _SysUser2RoleOID = value; }        }        private static NormalField __SysUser2RoleOID = new NormalField("SysUser2Role", "SysUser2RoleOID");        /// <summary>        /// SysUser2RoleOID        /// </summary>        [PrimaryKey()]        public static NormalField SysUser2RoleOID_        {            get { return __SysUser2RoleOID; }        }        private string _SysUserOID;        /// <summary>        /// SysUserOID        /// </summary>        public string SysUserOID        {            get { return _SysUserOID; }            set { _SysUserOID = value; }        }        private static NormalField __SysUserOID = new NormalField("SysUser2Role", "SysUserOID");        /// <summary>        /// SysUserOID        /// </summary>        public static NormalField SysUserOID_        {            get { return __SysUserOID; }        }        private string _SysRoleOID;        /// <summary>        /// SysRoleOID        /// </summary>        public string SysRoleOID        {            get { return _SysRoleOID; }            set { _SysRoleOID = value; }        }        private static NormalField __SysRoleOID = new NormalField("SysUser2Role", "SysRoleOID");        /// <summary>        /// SysRoleOID        /// </summary>        public static NormalField SysRoleOID_        {            get { return __SysRoleOID; }        }        private string _CreateUserOID;        /// <summary>        /// CreateUserOID        /// </summary>        public string CreateUserOID        {            get { return _CreateUserOID; }            set { _CreateUserOID = value; }        }        private static NormalField __CreateUserOID = new NormalField("SysUser2Role", "CreateUserOID");        /// <summary>        /// CreateUserOID        /// </summary>        public static NormalField CreateUserOID_        {            get { return __CreateUserOID; }        }        private DateTime? _UpdateTime;        /// <summary>        /// UpdateTime        /// </summary>        public DateTime? UpdateTime        {            get { return _UpdateTime; }            set { _UpdateTime = value; }        }        private static NormalField __UpdateTime = new NormalField("SysUser2Role", "UpdateTime");        /// <summary>        /// UpdateTime        /// </summary>        public static NormalField UpdateTime_        {            get { return __UpdateTime; }        }        private static FieldAll _All;        public static FieldAll All        {            get            {                _All = _All ?? new FieldAll(typeof(SysUser2Role));                return _All;            }        }    }}namespace ORM.Demo.Model {    using System;    using ORM;        public partial class SysRole : TableEntity {        private Guid? _SysRoleOID;        /// <summary>        /// SysRoleOID        /// </summary>        public Guid? SysRoleOID        {            get { return _SysRoleOID; }            set { _SysRoleOID = value; }        }        private static NormalField __SysRoleOID = new NormalField("SysRole", "SysRoleOID");        /// <summary>        /// SysRoleOID        /// </summary>        [PrimaryKey()]        public static NormalField SysRoleOID_        {            get { return __SysRoleOID; }        }        private string _RoleName;        /// <summary>        /// RoleName        /// </summary>        public string RoleName        {            get { return _RoleName; }            set { _RoleName = value; }        }        private static NormalField __RoleName = new NormalField("SysRole", "RoleName");        /// <summary>        /// RoleName        /// </summary>        public static NormalField RoleName_        {            get { return __RoleName; }        }        private string _CreateUserOID;        /// <summary>        /// CreateUserOID        /// </summary>        public string CreateUserOID        {            get { return _CreateUserOID; }            set { _CreateUserOID = value; }        }        private static NormalField __CreateUserOID = new NormalField("SysRole", "CreateUserOID");        /// <summary>        /// CreateUserOID        /// </summary>        public static NormalField CreateUserOID_        {            get { return __CreateUserOID; }        }        private string _Remark;        /// <summary>        /// Remark        /// </summary>        public string Remark        {            get { return _Remark; }            set { _Remark = value; }        }        private static NormalField __Remark = new NormalField("SysRole", "Remark");        /// <summary>        /// Remark        /// </summary>        public static NormalField Remark_        {            get { return __Remark; }        }        private DateTime? _UpdateTime;        /// <summary>        /// UpdateTime        /// </summary>        public DateTime? UpdateTime        {            get { return _UpdateTime; }            set { _UpdateTime = value; }        }        private static NormalField __UpdateTime = new NormalField("SysRole", "UpdateTime");        /// <summary>        /// UpdateTime        /// </summary>        public static NormalField UpdateTime_        {            get { return __UpdateTime; }        }        private static FieldAll _All;        public static FieldAll All        {            get            {                _All = _All ?? new FieldAll(typeof(SysRole));                return _All;            }        }    }}namespace ORM.Demo.Model {    using System;    using ORM;        public partial class SysUser2Role : TableEntity {        private Guid? _SysUser2RoleOID;        /// <summary>        /// SysUser2RoleOID        /// </summary>        public Guid? SysUser2RoleOID        {            get { return _SysUser2RoleOID; }            set { _SysUser2RoleOID = value; }        }        private static NormalField __SysUser2RoleOID = new NormalField("SysUser2Role", "SysUser2RoleOID");        /// <summary>        /// SysUser2RoleOID        /// </summary>        [PrimaryKey()]        public static NormalField SysUser2RoleOID_        {            get { return __SysUser2RoleOID; }        }        private string _SysUserOID;        /// <summary>        /// SysUserOID        /// </summary>        public string SysUserOID        {            get { return _SysUserOID; }            set { _SysUserOID = value; }        }        private static NormalField __SysUserOID = new NormalField("SysUser2Role", "SysUserOID");        /// <summary>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表