SqlHelper简单实现(通过Expression和反射)2.特性和实体设计
生活随笔
收集整理的這篇文章主要介紹了
SqlHelper简单实现(通过Expression和反射)2.特性和实体设计
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
對于需求中的不要暴露DataTable或DataSet,我想到了設(shè)計(jì)中常用的對象:實(shí)體(Entity),通過實(shí)體將數(shù)據(jù)庫中的字段封裝成類,這樣做不僅使代碼更有可讀性,維護(hù)起來也很方便。同時(shí)我自定義了一些C#特性來表述字段在數(shù)據(jù)庫中的特性。
1.遞增鍵:
1 namespace RA.DataAccess.Attributes 2 { 3 /// <summary> 4 /// 遞增鍵 5 /// </summary> 6 [AttributeUsage(AttributeTargets.Property, Inherited = true)] 7 public class IdentityAttribute:Attribute 8 { 9 } 10 }2.主鍵:
1 namespace RA.DataAccess.Attributes 2 { 3 /// <summary> 4 /// 主鍵 5 /// </summary> 6 [AttributeUsage(AttributeTargets.Property, Inherited = true)] 7 public class PrimaryAttribute : Attribute 8 { 9 10 } 11 }3.表名:
1 namespace RA.DataAccess.Attributes 2 { 3 /// <summary> 4 /// 表名 5 /// </summary> 6 [AttributeUsage(AttributeTargets.Class, Inherited = true)] 7 public class TableNameAttribute : Attribute 8 { 9 public string TableName { get; set; } 10 11 public TableNameAttribute(string name) 12 { 13 TableName = name; 14 } 15 } 16 }?測試用例:
1 namespace RA.MyBlog.Entity 2 { 3 4 [TableName("RA_MyBlog_Article")] 5 public class ArticleEntity 6 { 7 [Primary] 8 [Identity] 9 //文章ID 10 public int articleID { get; set; } 11 //分類ID 12 public int categoryID { get; set; } 13 //文章標(biāo)題 14 public string articleTitle { get; set; } 15 //文章版權(quán) 16 public string articleCopyright { get; set; } 17 //文章創(chuàng)建時(shí)間 18 public DateTime articleDate { get; set; } 19 //文章摘要 20 public string articleAbstract { get; set; } 21 //文章內(nèi)容 22 public string articleContain { get; set; } 23 //文章所屬User 24 public int userID { get; set; } 25 } 26 }?
轉(zhuǎn)載于:https://www.cnblogs.com/kakura/p/6108858.html
總結(jié)
以上是生活随笔為你收集整理的SqlHelper简单实现(通过Expression和反射)2.特性和实体设计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nodejs实现的简单接口
- 下一篇: Python--三元运算与lambda表