zoukankan      html  css  js  c++  java
  • SqlHelper简单实现(通过Expression和反射)2.特性和实体设计

    对于需求中的不要暴露DataTable或DataSet,我想到了设计中常用的对象:实体(Entity),通过实体将数据库中的字段封装成类,这样做不仅使代码更有可读性,维护起来也很方便。同时我自定义了一些C#特性来表述字段在数据库中的特性。

    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         //文章标题
    14         public string articleTitle { get; set; }
    15         //文章版权
    16         public string articleCopyright { get; set; }
    17         //文章创建时间
    18         public DateTime articleDate { get; set; }
    19         //文章摘要
    20         public string articleAbstract { get; set; }
    21         //文章内容
    22         public string articleContain { get; set; }
    23         //文章所属User
    24         public int userID { get; set; }
    25     }
    26 }
  • 相关阅读:
    Oracle 验证A表的2个字段组合不在B表2个字段组合里的数据
    jQuery方法一览
    Maven构建项目时index.jsp文件报错
    DDL——对数据库表的结构进行操作的练习
    不经意的小错误——onclick和click的区别
    UML基础——统一建模语言简介
    基于UML的面向对象分析与设计
    数据结构之——树与二叉树
    UML类图几种关系的总结
    C3P0连接参数解释
  • 原文地址:https://www.cnblogs.com/kakura/p/6108858.html
Copyright © 2011-2022 走看看