自己寫數(shù)據(jù)庫訪問ORM
作者:流沙inaction
目前,往上有很多優(yōu)秀的ORM,但和Csla緊密結(jié)合還沒找到合適的。出于需要,自己動手寫了一個。主要優(yōu)點在于,實現(xiàn)了直接通過DataReader填充業(yè)務類數(shù)據(jù)。IF使用簡單,只要在業(yè)務類上標記特性TableClass、FieldDescription即可將業(yè)務類和數(shù)據(jù)庫建立映射關(guān)系。
下面看一個例子:
現(xiàn)在有一個用戶信息的表:E-R圖如下:
要實現(xiàn)該表的數(shù)據(jù)庫新增、修改、查詢功能,需要實現(xiàn)下面兩個業(yè)務類:
- using Csla;
- using IF.CslaCore;
- using IF.OrmCore.DataSchema;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace IF.SysUser.Business
- {
- [Serializable]
- [TableClass(FriendlyName="用戶信息表",TableName="SYS_USER")]
- public class SysUser : IfBusiness<SysUser>
- {
- private static readonly PropertyInfo<string> SUR_IDProperty = RegisterProperty<string>(c => c.SUR_ID);
- [DisplayName("SUR_ID")]
- [FieldDescription(IsPrimaryKey=true,ColumnName="SUR_ID",FriendlyName="SUR_ID",NeedUpdate=true)]
- public string SUR_ID { get; set; }
- private static readonly PropertyInfo<string> UserNameProperty = RegisterProperty<string>(c => c.UserName);
- [DisplayName("登錄名")]
- [FieldDescription(ColumnName="SUR_USERNAME",FriendlyName="登錄名",NeedUpdate=true)]
- public string UserName { get; set; }
- private static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
- [DisplayName("姓名")]
- [FieldDescription(ColumnName="SUR_NAME",FriendlyName="姓名",NeedUpdate=true)]
- public string Name { get; set; }
- private static readonly PropertyInfo<string> PasswordProperty = RegisterProperty<string>(c => c.Password);
- [DisplayName("密碼")]
- [FieldDescription(ColumnName="SUR_PASSWORD",FriendlyName="密碼",NeedUpdate=true)]
- public string Password { get; set; }
- private static readonly PropertyInfo<string> LoginMacProperty = RegisterProperty<string>(c => c.LoginMac);
- [DisplayName("登錄Mac地址")]
- [FieldDescription(ColumnName="SUR_LOGIN_MAC",FriendlyName="登錄Mac地址",NeedUpdate=true)]
- public string LoginMac { get; set; }
- private static readonly PropertyInfo<string> LoginIPProperty = RegisterProperty<string>(c => c.LoginIP);
- [DisplayName("登錄IP")]
- [FieldDescription(ColumnName="SUR_LOGIN_IP",FriendlyName="登錄IP",NeedUpdate=true)]
- public string LoginIP { get; set; }
- private static readonly PropertyInfo<DateTime?> LoginTimeProperty = RegisterProperty<DateTime?>(c => c.LoginTime);
- [DisplayName("登錄時間")]
- [FieldDescription(ColumnName="SUR_LOGIN_TIME",FriendlyName="登錄時間",NeedUpdate=true)]
- public DateTime? LoginTime { get; set; }
- private static readonly PropertyInfo<DateTime?> LogoutTimeProperty = RegisterProperty<DateTime?>(c => c.LogoutTime);
- [DisplayName("登出時間")]
- [FieldDescription(ColumnName="SUR_LOGOUT_TIME",FriendlyName="登出時間",NeedUpdate=true)]
- public DateTime? LogoutTime { get; set; }
- private static readonly PropertyInfo<DateTime?> LoginFailTimeProperty = RegisterProperty<DateTime?>(c => c.LoginFailTime);
- [DisplayName("登錄失敗時間")]
- [FieldDescription(ColumnName="SUR_LOGIN_FAIL_TIME",FriendlyName="登錄失敗時間",NeedUpdate=true)]
- public DateTime? LoginFailTime { get; set; }
- private static readonly PropertyInfo<Int32?> LoginFailCountProperty = RegisterProperty<Int32?>(c => c.LoginFailCount);
- [DisplayName("登錄失敗次數(shù)")]
- [FieldDescription(ColumnName="SUR_LOGIN_FAIL_COUNT",FriendlyName="登錄失敗次數(shù)",NeedUpdate=true)]
- public Int32? LoginFailCount { get; set; }
- private static readonly PropertyInfo<bool?> LockFGProperty = RegisterProperty<bool?>(c => c.LockFG);
- [DisplayName("是否鎖定")]
- [FieldDescription(ColumnName="SUR_LOCK_FG",FriendlyName="是否鎖定",NeedUpdate=true)]
- public bool? LockFG { get; set; }
- private static readonly PropertyInfo<bool?> DisableFGProperty = RegisterProperty<bool?>(c => c.DisableFG);
- [DisplayName("是否有效")]
- [FieldDescription(ColumnName="SUR_DISABLE_FG",FriendlyName="是否有效",NeedUpdate=true)]
- public bool? DisableFG { get; set; }
- #region 通用字段
- private static readonly PropertyInfo<DateTime?> CreateTimeProperty = RegisterProperty<DateTime?>(c => c.CreateTime);
- [DisplayName("創(chuàng)建時間")]
- [FieldDescription(ColumnName="CreateTime",FriendlyName="創(chuàng)建時間",NeedUpdate=true)]
- public override DateTime? CreateTime { get; set; }
- private static readonly PropertyInfo<DateTime?> LastUpdateTimeProperty = RegisterProperty<DateTime?>(c => c.LastUpdateTime);
- [DisplayName("***修改時間")]
- [FieldDescription(ColumnName="LastUpdateTime",FriendlyName="***修改時間",NeedUpdate=true)]
- public override DateTime? LastUpdateTime { get; set; }
- public override void SetPrimaryKey(string key)
- {
- SUR_ID = key;
- }
- #endregion
- }
- [Serializable]
- public class SysUserList : IfBusinessList<SysUserList, SysUser>
- { }
- }
現(xiàn)在就可以工作了:
全表檢索數(shù)據(jù)方法:
- SysUserList selData = SysUserList.Fetch();
向數(shù)據(jù)庫新增一條數(shù)據(jù):
- SysUser.Business.SysUser user = new SysUser.Business.SysUser();
- user.UserName= "inaction";
- user.Name = "流砂";
- user.Password= "superman";
- selData.Add(user);
- selData.Save();
向數(shù)據(jù)庫修改數(shù)據(jù):
- var user = SysUserList.Fetch(c => c.UserName == "inaction");
- user.Password = "123456";
- SysUserList list = new SysUserList { user };
- list.Save();
以上代碼就實現(xiàn)了對密碼的修改。
特別說明:目前IF 只能通過SysUserList對象的Save方法保存數(shù)據(jù)。以后會實現(xiàn)通過業(yè)務類自身Save方法保存數(shù)據(jù)。
責任編輯:彭凡
來源:
51CTO