程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 多態的應用

多態的應用

編輯:關於.NET

最近在寫學校的SRTP項目--學生信息管理系統,其中涉及到對以下信息的數據庫操作。當然持久化之前要對數據的合法性進行驗證,如果非法要提示合理信息,如果有其他問題會拋出異常。

這些信息幾乎按種來分類,每類一個數據表,也就是一類實體,除了各個實體屬性不同其余操作就相差不大了。那麼怎樣來對這些信息進行統一的操作呢?這裡就用到了多態。下面就用我的實現來作為例子吧,如果大家有什麼好的想法可以提出來,不對的地方希望大家指出。謝謝,呵呵!

涉及到信息:

1.基本信息:學號,姓名,班級,專業,年齡,身份證號,籍貫等

2.家庭信息:家庭住址,家庭電話,家庭成員信息等

3.獎學金信息:包括獲得獎學金的數額,時間,項目等

4.活動信息:參加的活動,活動舉行的時間,活動的結果等

5.資助信息:資助的項目,資助金額,資助時間等

6.處分信息:處分的時間,原因,是否被撤銷等

7.素質測評信息:包括測評的時間,文體,時間等的得分情況

8.參加的比賽信息:包括比賽名稱,時間,結果等

實現概括:

每類信息的添加,修改都有一個專門的VIEW,也就是一個WinForm,他們共同繼承Form_base,base處理共同問題,具體問題子類各自處理

Code

1using System;
   2using System.Collections.Generic;
   3using System.ComponentModel;
   4using System.Data;
   5using System.Drawing;
   6using System.Linq;
   7using System.Text;
   8using System.Windows.Forms;
   9
  10using StudentManagerV3.DataBase;
  11
  12namespace StudentManagerV3
  13{
  14    public partial class Form_Base : Form
  15    {
  16        private StudentManagerDataContext context;
  17        private bool isEidt = false;
  18        /**//// <summary>
  19        /// 視圖的引用,當模型發生改變時更新視圖,呈獻給用戶新結果
  20        /// </summary> 
  21        protected DataGridView GridView { get; set; }
  22
  23        /**//// <summary>
  24        /// 如果是編輯信息,實體不為空,強制轉換成用戶需要的實體
  25        /// </summary>
  26        protected object Entity { get; set; }
  27
  28        /**//// <summary>
  29        /// 每個Form都單獨維護自己的一個Context,防止產生沖突或者引發錯誤
  30        /// </summary>
  31        protected StudentManagerDataContext Context
  32        {
  33            get
  34            {
  35                if (context == null) throw new ArgumentNullException();
  36                return context;
  37            }
  38        }
  39
  40        /**//// <summary>
  41        /// 標識是編輯信息還是添加新信息,
  42        /// </summary>
  43        protected bool IsEdit { get { return isEidt; } }
  44
  45        public Form_Base()
  46        { }
  47
  48        public Form_Base(object entity)
  49        {
  50            if (entity != null) { isEidt = true; this.Entity = entity; }
  51            //AppInfo包含了系統的配置信息
  52            context = new StudentManagerDataContext(AppInfo.ConnectionString);
  53        }
  54
  55        //判斷當前操作的實體是否有效
  56        protected bool ISLegal()
  57        {
  58            //全局信息,標識當前實體
  59            return DataPool.StudentNumber != "0";
  60        }
  61
  62        public void ShowHandleEntityError(string message)
  63        {
  64            if (message != null)
  65                SMSApplication.WriteLogAndShowSuggestiveInfo(message,
  66                    SysConfig.Document.SelectNodes(XmlSelectorInfo.ApplicationError)[0].InnerText);
  67            else SMSApplication.WriteLogAndShowSuggestiveInfo(message,
  68                SysConfig.Document.SelectNodes(XmlSelectorInfo.EntityError)[0].InnerText);
  69        }
  70
  71        /**//// <summary>
  72        /// 添加,修改學生信息(除基本信息)的邏輯,統一處理(多態的實現)
  73        /// </summary>
  74        protected void AddOrEditInfo()
  75        {
  76            try
  77            {
  78                if (ISLegal())
  79                {
  80                    ConstructEntityInfo();
  81                    SMSApplication.ShowOperateSucceedResult();
  82                }
  83                else ShowHandleEntityError(null); ;
  84            }
  85            catch (FormatException exp)
  86            {
  87                //SMSApplication系統寫入日志和顯示友好信息的組件,XmlSelectorInfo記錄xml配置文件尋址信息
  88                SMSApplication.WriteLogAndShowSuggestiveInfo(exp.Message, SysConfig.Document.SelectNodes(XmlSelectorInfo.FormatError)[0].InnerText);
  89            }
  90            catch (OverflowException exo)
  91            {
  92                SMSApplication.WriteLogAndShowSuggestiveInfo(exo.Message, SysConfig.Document.SelectNodes(XmlSelectorInfo.OverFlowError)[0].InnerText);
  93            }
  94            catch (Exception ex)
  95            {
  96                ShowHandleEntityError(ex.Message);
  97            }
  98            this.Dispose();
  99        }
100        /**//// <summary>
101        /// 獲得新信息實體的方法,由子類實現
102        /// </summary>
103        protected virtual void ConstructEntityInfo()
104        {
105            throw new NotImplementedException();
106        }
107
108    }
109}
110

其中幾個子類的實現:

Code

1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Linq;
  7using System.Text;
  8using System.Windows.Forms;
  9
10using StudentManagerV3.DataBase;
11
12namespace StudentManagerV3
13{
14    public partial class Form_Game : Form_Base
15    {
16        /**//// <summary>
17        /// 修改添加時對應的實體
18        /// </summary>
19        private Game game = new Game();
20
21        public Form_Game(DataGridView view, object entity)
22            : base(entity)
23        {
24            //這個通過Base放到父類中更合適,這樣每個子類又減少了代碼量,放到這裡也算是錯誤代碼的樣例介紹把。呵呵
25            this.GridView = view;
26            this.Entity = entity;
27
28            InitializeComponent();
29
30            //如果是編輯,自然要初始化控件內容
31            if (IsEdit)
32            {
33                game = entity as Game;
34                dateTimePicker_GameTime.Text = game.GameDateTime.ToString();
35                textBox_GameName.Text = game.GameName;
36                textBox_GameResult.Text = game.GameResult;
37            }
38        }
39
40        private void button_AddGame_Click(object sender, EventArgs e)
41        {
42            //調用父類的邏輯
43            AddOrEditInfo();
44        }
45        /**//// <summary>
46        /// 實現父類的方法
47        /// </summary>
48        protected override void ConstructEntityInfo()
49        {
50            if (IsEdit)
51                game = (from ga in Context.Games where ga.GameID == ((Game)Entity).GameID select ga).Single();
52            game.GameResult = textBox_GameResult.Text;
53            game.GameName = textBox_GameName.Text;
54            game.GameDateTime = Convert.ToDateTime(dateTimePicker_GameTime.Text);
55            if (!IsEdit)
56            {
57                game.StudentNumber = DataPool.StudentNumber;
58                Context.Games.InsertOnSubmit(game);
59            }
60            Context.SubmitChanges();
61            GridView.DataSource = GetIQueryable.GetGameQueryable(Context);
62        }
63
64        private void button_CancleGame_Click(object sender, EventArgs e)
65        {
66            this.Dispose();
67        }
68    }
69}
70

Code

1using System;
  2using System.Linq;
  3using System.Windows.Forms;
  4
  5using StudentManagerV3.DataBase;
  6
  7namespace StudentManagerV3
  8{
  9    /**//// <summary>
10    /// 注釋沒寫,同上一個類差不多
11    /// </summary>
12    public partial class Form_Support : Form_Base
13    {
14        private StudentSupport support = new StudentSupport();
15
16        public Form_Support(DataGridView gridview, object entity)
17            : base(entity)
18        {
19            this.GridView = gridview;
20            this.Entity = entity;
21            InitializeComponent();
22            if (IsEdit)
23            {
24                support = entity as StudentSupport;
25                dateTimePicker_SupportTime.Text = support.SupportTime.ToShortDateString();
26                textBox_SupportAmount.Text = support.SupportAmount.ToString();
27                textBox_SupportRemark.Text = support.SupportRemark;
28                textBox_SupportSource.Text = support.SupportFrom;
29            }
30        }
31
32        private void button_AddSupport_Click(object sender, EventArgs e)
33        {
34            AddOrEditInfo();
35        }
36
37        protected override void ConstructEntityInfo()
38        {
39            if (IsEdit)
40                support = (from su in Context.StudentSupports where su.SupportID == ((StudentSupport)Entity).SupportID select su).Single();
41            support.SupportAmount = Convert.ToInt32(textBox_SupportAmount.Text);
42            support.SupportFrom = textBox_SupportSource.Text;
43            support.SupportRemark = textBox_SupportRemark.Text;
44            support.SupportTime = Convert.ToDateTime(dateTimePicker_SupportTime.Text);
45            if (!IsEdit)
46            {
47                support.StudentNumber = DataPool.StudentNumber;
48                Context.StudentSupports.InsertOnSubmit(support);
49            }
50            Context.SubmitChanges();
51            GridView.DataSource = GetIQueryable.GetSupportQueryable(Context);
52        }
53
54        private void button_CancleSupport_Click(object sender, EventArgs e)
55        {
56            this.Dispose();
57        }
58    }
59}
60

多態的實現基本就是子類中調用AddOrEditInfo()的過程了。呵呵 ,就到這裡拉

主頁:http://jingtao.cnblogs.com

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