当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 基于.NET平台的分层架构实战(四)实体类的设计与实现

ASP.NET
asp.net GridView控件中模板列CheckBox全选、反选、取消
asp.net GridView 删除时弹出确认对话框(包括内容提示)
asp.net DropDownList 三级联动下拉菜单实现代码
asp DataTable添加列和行的三种方法
Asp.net 页面调用javascript变量的值
asp.net 长文章通过设定的行数分页
asp.net 定时间点执行任务的简易解决办法
asp.net 页面延时五秒,跳转到另外的页面
asp.net 动态输出透明gif图片
asp.net DataList与Repeater用法区别
asp.net Javascript获取CheckBoxList的value
asp.net程序在调式和发布之间图片路径问题的解决方法
asp.net下生成英文字符数字验证码的代码
asp.net 页面版文本框智能提示JSCode (升级版)
ASP.NET URL伪静态重写实现方法
ASP.NET 2.0 中Forms安全认证
asp.net 动态添加多个用户控件
asp.net Repeater显示父子表数据,无闪烁
asp.net 无法获取的内部内容,因为该内容不是文本 的解决方法
asp.net GridView排序简单实现

ASP.NET 中的 基于.NET平台的分层架构实战(四)实体类的设计与实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 79 ::
收藏到网摘: n/a

实体类是现实实体在计算机中的表示。它贯穿于整个架构,负担着在各层次及模块间传递数据的职责。一般来说,实体类可以分为“贫血实体类”和“充血实体类”,前者仅仅保存实体的属性,而后者还包含一些实体间的关系与逻辑。我们在这个Demo中用的实体类将是“贫血实体类”。
  
   大多情况下,实体类和数据库中的表(这里指实体表,不包括表示多对多对应的关系表)是一一对应的,但这并不是一个限制,在复杂的数据库设计中,有可能出现一个实体类对应多个表,或者交叉对应的情况。在本文的Demo中,实体类和表是一一对应的,并且实体类中的属性和表中的字段也是对应的。
  
   在看实体类的代码前,先看一下系统的工程结构。
  
  如上图所示,在初始阶段,整个系统包括6个工程,它们的职责是这样的:
   Web——表示层
   Entity——存放实体类
   Factory——存放和依赖注入及IoC相关的类
   IBLL——存放业务逻辑层接口族
   IDAL——存放数据访问层接口族
   Utility——存放各种工具类及辅助类
  
   这只是一个初期架构,主要是将整个系统搭一个框架,在后续开发中,将会有其他工程被陆陆续续添加进来。
  
   我们的实体类将放在Entity工程下,这里包括三个文件:AdminInfo.cs,MessageInfo.cs,CommentInfo.cs,分别是管理员实体类、留言实体类和评论实体类。具体代码如下:
  
  AdminInfo.cs:
  AdminInfo
   1using System;
   2
   3namespace NGuestBook.Entity
   4{
   5 /**//// <summary>
   6 /// 实体类-管理员
   7 /// </summary>
   8 [Serializable]
   9 public class AdminInfo
  10 {
  11 private int id;
  12 private string name;
  13 private string password;
  14
  15 public int ID
  16 {
  17 get { return this.id; }
  18 set { this.id = value; }
  19 }
  20
  21 public string Name
  22 {
  23 get { return this.name; }
  24 set { this.name = value; }
  25 }
  26
  27 public string Password
  28 {
  29 get { return this.password; }
  30 set { this.password = value; }
  31 }
  32 }
  33}
  34
  MessageInfo.cs:
  MessageInfo
   1using System;
   2
   3namespace NGuestBook.Entity
   4{
   5 /**//// <summary>
   6 /// 实体类-留言
   7 /// </summary>
   8 [Serializable]
   9 public class MessageInfo
  10 {
  11 private int id;
  12 private string guestName;
  13 private string guestEmail;
  14 private string content;
  15 private DateTime time;
  16 private string reply;
  17 private string isPass;
  18
  19 public int ID
  20 {
  21 get { return this.id; }
  22 set { this.id = value; }
  23 }
  24
  25 public string GuestName
  26 {
  27 get { return this.guestName; }
  28 set { this.guestName = value; }
  29 }
  30
  31 public string GuestEmail
  32 {
  33 get { return this.guestEmail; }
  34 set { this.guestEmail = value; }
  35 }
  36
  37 public string Content
  38 {
  39 get { return this.content; }
  40 set { this.content = value; }
  41 }
  42
  43 public DateTime Time
  44 {
  45 get { return this.time; }
  46 set { this.time = value; }
  47 }
  48
  49 public string Reply
  50 {
  51 get { return this.reply; }
  52 set { this.reply = value; }
  53 }
  54
  55 public string IsPass
  56 {
  57 get { return this.isPass; }
  58 set { this.isPass = value; }
  59 }
  60 }
  61}
  62
  CommentInfo.cs:
  CommentInfo
   1using System;
   2
   3namespace NGuestBook.Entity
   4{
   5 /**//// <summary>
   6 /// 实体类-评论
   7 /// </summary>
   8 [Serializable]
   9 public class CommentInfo
  10 {
  11 private int id;
  12 private string content;
  13 private DateTime time;
  14 private int message;
  15
  16 public int ID
  17 {
  18 get { return this.id; }
  19 set { this.id = value; }
  20 }
  21
  22 public string Content
  23 {
  24 get { return this.content; }
  25 set { this.content = value; }
  26 }
  27
  28 public DateTime Time
  29 {
  30 get { return this.time; }
  31 set { this.time = value; }
  32 }
  33
  34 public int Message
  35 {
  36 get { return this.message; }
  37 set { this.message = value; }
  38 }
  39 }
  40}
  41
   大家可以看出,实体类的代码很简单,仅仅是负责实体的表示和数据的传递,不包含任何逻辑性内容。