• [织梦吧]唯一域名:www.dedecms8.com,织梦DedeCMS学习平台.

当前位置: > 编程与数据库 > net编程 >

多态的应用

来源: www.dedecms8.com 编辑:织梦吧 时间:2012-06-27点击:

 最近在写学校的SRTP项目--学生信息管理系统,其中涉及到对以下信息的数据库操作。当然持久化之前要对数据的合法性进行验证,如果非法要提示合理信息,如果有其他问题会抛出异常。

这些信息几乎按种来分类,每类一个数据表,也就是一类实体,除了各个实体属性不同其余操作就相差不大了。那么怎样来对这些信息进行统一的操作呢?这里就用到了多态。下面就用我的实现来作为例子吧,如果大家有什么好的想法可以提出来,不对的地方希望大家指出。谢谢,呵呵!

涉及到信息:

1.基本信息:学号,姓名,班级,专业,年龄,身份证号,籍贯等

2.家庭信息:家庭住址,家庭电话,家庭成员信息等

3.奖学金信息:包括获得奖学金的数额,时间,项目等

4.活动信息:参加的活动,活动举行的时间,活动的结果等

5.资助信息:资助的项目,资助金额,资助时间等

6.处分信息:处分的时间,原因,是否被撤销等

7.素质测评信息:包括测评的时间,文体,时间等的得分情况

8.参加的比赛信息:包括比赛名称,时间,结果等

实现概括:

每类信息的添加,修改都有一个专门的VIEW,也就是一个WinForm,他们共同继承Form_base,base处理共同问题,具体问题子类各自处理

From_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()的过程了。呵呵 ,就到这里拉

About D8

  • ©2014 织梦吧(d8) DedeCMS学习交流平台
  • 唯一网址 www.DedeCMS8.com 网站地图
  • 联系我们 1978130638@qq.com ,  QQ