程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 基於.NET平台的分層架構實戰(四)—實體類的設計與實現

基於.NET平台的分層架構實戰(四)—實體類的設計與實現

編輯:關於.NET

實體類是現實實體在計算機中的表示。它貫穿於整個架構,負擔著在各層次及 模塊間傳遞數據的職責。一般來說,實體類可以分為“貧血實體類” 和“充血實體類”,前者僅僅保存實體的屬性,而後者還包含一些實 體間的關系與邏輯。我們在這個Demo中用的實體類將是“貧血實體類 ”。

大多情況下,實體類和數據庫中的表(這裡指實體表,不包括 表示多對多對應的關系表)是一一對應的,但這並不是一個限制,在復雜的數據 庫設計中,有可能出現一個實體類對應多個表,或者交叉對應的情況。在本文的 Demo中,實體類和表是一一對應的,並且實體類中的屬性和表中的字段也是對應 的。

在看實體類的代碼前,先看一下系統的工程結構。

基於.NET平台的分層架構實戰(四)——實
體類的設計與實現

如上圖所示,在初始階段,整個系統包括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

大家可以看出,實體類的代碼很簡單,僅僅是負責實體的表示和數據的傳遞,不包含任何邏輯性 內容。下篇將介紹接口的設計。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved