博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己写数据库访问ORM
阅读量:4613 次
发布时间:2019-06-09

本文共 5247 字,大约阅读时间需要 17 分钟。

目前,往上有很多优秀的ORM,但和Csla紧密结合还没找到合适的。出于需要,自己动手写了一个。主要优点在于,实现了直接通过DataReader填充业务类数据。IF使用简单,只要在业务类上标记特性TableClass、FieldDescription即可将业务类和数据库建立映射关系。

下面看一个例子:

现在有一个用户信息的表:E-R图如下:

 

要实现该表的数据库新增、修改、查询功能,需要实现下面两个业务类:

1 using Csla;  2 using IF.CslaCore;  3 using IF.OrmCore.DataSchema;  4 using System;  5 using System.Collections.Generic;  6 using System.ComponentModel;  7 using System.Linq;  8 using System.Text;  9 using System.Threading.Tasks; 10  11 namespace IF.SysUser.Business 12 { 13     [Serializable] 14     [TableClass(FriendlyName="用户信息表",TableName="SYS_USER")] 15     public class SysUser : IfBusiness
16 { 17 private static readonly PropertyInfo
SUR_IDProperty = RegisterProperty
(c => c.SUR_ID); 18 19 [DisplayName("SUR_ID")] 20 [FieldDescription(IsPrimaryKey=true,ColumnName="SUR_ID",FriendlyName="SUR_ID",NeedUpdate=true)] 21 public string SUR_ID { get; set; } 22 23 private static readonly PropertyInfo
UserNameProperty = RegisterProperty
(c => c.UserName); 24 [DisplayName("登录名")] 25 [FieldDescription(ColumnName="SUR_USERNAME",FriendlyName="登录名",NeedUpdate=true)] 26 public string UserName { get; set; } 27 28 29 private static readonly PropertyInfo
NameProperty = RegisterProperty
(c => c.Name); 30 [DisplayName("姓名")] 31 [FieldDescription(ColumnName="SUR_NAME",FriendlyName="姓名",NeedUpdate=true)] 32 public string Name { get; set; } 33 34 private static readonly PropertyInfo
PasswordProperty = RegisterProperty
(c => c.Password); 35 [DisplayName("密码")] 36 [FieldDescription(ColumnName="SUR_PASSWORD",FriendlyName="密码",NeedUpdate=true)] 37 public string Password { get; set; } 38 39 private static readonly PropertyInfo
LoginMacProperty = RegisterProperty
(c => c.LoginMac); 40 [DisplayName("登录Mac地址")] 41 [FieldDescription(ColumnName="SUR_LOGIN_MAC",FriendlyName="登录Mac地址",NeedUpdate=true)] 42 public string LoginMac { get; set; } 43 44 private static readonly PropertyInfo
LoginIPProperty = RegisterProperty
(c => c.LoginIP); 45 [DisplayName("登录IP")] 46 [FieldDescription(ColumnName="SUR_LOGIN_IP",FriendlyName="登录IP",NeedUpdate=true)] 47 public string LoginIP { get; set; } 48 49 50 private static readonly PropertyInfo
LoginTimeProperty = RegisterProperty
(c => c.LoginTime); 51 [DisplayName("登录时间")] 52 [FieldDescription(ColumnName="SUR_LOGIN_TIME",FriendlyName="登录时间",NeedUpdate=true)] 53 public DateTime? LoginTime { get; set; } 54 55 private static readonly PropertyInfo
LogoutTimeProperty = RegisterProperty
(c => c.LogoutTime); 56 [DisplayName("登出时间")] 57 [FieldDescription(ColumnName="SUR_LOGOUT_TIME",FriendlyName="登出时间",NeedUpdate=true)] 58 public DateTime? LogoutTime { get; set; } 59 60 private static readonly PropertyInfo
LoginFailTimeProperty = RegisterProperty
(c => c.LoginFailTime); 61 [DisplayName("登录失败时间")] 62 [FieldDescription(ColumnName="SUR_LOGIN_FAIL_TIME",FriendlyName="登录失败时间",NeedUpdate=true)] 63 public DateTime? LoginFailTime { get; set; } 64 65 private static readonly PropertyInfo
LoginFailCountProperty = RegisterProperty
(c => c.LoginFailCount); 66 [DisplayName("登录失败次数")] 67 [FieldDescription(ColumnName="SUR_LOGIN_FAIL_COUNT",FriendlyName="登录失败次数",NeedUpdate=true)] 68 public Int32? LoginFailCount { get; set; } 69 70 71 private static readonly PropertyInfo
LockFGProperty = RegisterProperty
(c => c.LockFG); 72 [DisplayName("是否锁定")] 73 [FieldDescription(ColumnName="SUR_LOCK_FG",FriendlyName="是否锁定",NeedUpdate=true)] 74 public bool? LockFG { get; set; } 75 76 private static readonly PropertyInfo
DisableFGProperty = RegisterProperty
(c => c.DisableFG); 77 [DisplayName("是否有效")] 78 [FieldDescription(ColumnName="SUR_DISABLE_FG",FriendlyName="是否有效",NeedUpdate=true)] 79 public bool? DisableFG { get; set; } 80 81 82 #region 通用字段 83 84 private static readonly PropertyInfo
CreateTimeProperty = RegisterProperty
(c => c.CreateTime); 85 [DisplayName("创建时间")] 86 [FieldDescription(ColumnName="CreateTime",FriendlyName="创建时间",NeedUpdate=true)] 87 public override DateTime? CreateTime { get; set; } 88 89 private static readonly PropertyInfo
LastUpdateTimeProperty = RegisterProperty
(c => c.LastUpdateTime); 90 [DisplayName("最后修改时间")] 91 [FieldDescription(ColumnName="LastUpdateTime",FriendlyName="最后修改时间",NeedUpdate=true)] 92 public override DateTime? LastUpdateTime { get; set; } 93 94 95 96 public override void SetPrimaryKey(string key) 97 { 98 SUR_ID = key; 99 }100 #endregion101 }102 103 [Serializable]104 public class SysUserList : IfBusinessList
105 { }106 }
View Code

现在就可以工作了:

全表检索数据方法:

1 SysUserList selData = SysUserList.Fetch();
View Code

 

向数据库新增一条数据:

1 SysUser.Business.SysUser user = new SysUser.Business.SysUser(); 2  3 user.UserName= "inaction"; 4  5 user.Name = "流砂"; 6  7 user.Password= "superman"; 8  9 selData.Add(user);10 11 selData.Save();
View Code

 

向数据库修改数据:

1 var user = SysUserList.Fetch(c => c.UserName == "inaction");2 3 user.Password = "123456";4 5  SysUserList list = new SysUserList { user };6  list.Save();
View Code

以上代码就实现了对密码的修改。

特别说明:目前IF 只能通过SysUserList对象的Save方法保存数据。以后会实现通过业务类自身Save方法保存数据。

 

附:

 

 

转载于:https://www.cnblogs.com/nactioncsla/p/3439820.html

你可能感兴趣的文章
form表单里的坑
查看>>
Vs2010+opencv2.3.1 imread出现异常
查看>>
Restful --- 让JSON回归单纯
查看>>
★如何解释特修斯之船问题?
查看>>
循环移位
查看>>
第九章 LinkedBlockingQueue源码解析
查看>>
Android 自定义View - 启航 一般View定义
查看>>
Algorithm
查看>>
微积分初步
查看>>
毕业论文格式范例讲解
查看>>
js的块级作用域
查看>>
委托、Lambda表达式和事件
查看>>
typecho模板制作代码收集
查看>>
Python学习笔记4:集合方法
查看>>
POJ - 3696 同余
查看>>
[随想感悟] 《归去来兮辞·并序》 赏析
查看>>
elasticsearch的监控脚本
查看>>
USACO 之 Section 1.3 Greedy Algorithm (已解决)
查看>>
数组排序
查看>>
51Nod 1090: 3个数和为0
查看>>