最近在写学校的SRTP项目--学生信息管理系统,其中涉及到对以下信息的数据库操作。当然持久化之前要对数据的合法性进行验证,如果非法要提示合理信息,如果有其他问题会抛出异常。
这些信息几乎按种来分类,每类一个数据表,也就是一类实体,除了各个实体属性不同其余操作就相差不大了。那么怎样来对这些信息进行统一的操作呢?这里就用到了多态。下面就用我的实现来作为例子吧,如果大家有什么好的想法可以提出来,不对的地方希望大家指出。谢谢,呵呵!
涉及到信息:
1. 基本信息:学号,姓名,班级,专业,年龄,身份证号,籍贯等
2. 家庭信息:家庭住址,家庭电话,家庭成员信息等
3. 奖学金信息:包括获得奖学金的数额,时间,项目等
4. 活动信息:参加的活动,活动举行的时间,活动的结果等
5. 资助信息:资助的项目,资助金额,资助时间等
6. 处分信息:处分的时间,原因,是否被撤销等
7. 素质测评信息:包括测评的时间,文体,时间等的得分情况
8. 参加的比赛信息:包括比赛名称,时间,结果等
实现概括:
每类信息的添加,修改都有一个专门的VIEW,也就是一个WinForm,他们共同继承Form_base,base处理共同问题,具体问题子类各自处理
From_Base实现:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
using StudentManagerV3.DataBase;
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
namespace StudentManagerV3
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
14
public partial class Form_Base : Form
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
private StudentManagerDataContext context;
17
private bool isEidt = false;
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
19
/// 视图的引用,当模型发生改变时更新视图,呈献给用户新结果
20
/// </summary>
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected DataGridView GridView
{ get; set; }
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// 如果是编辑信息,实体不为空,强制转换成用户需要的实体
25
/// </summary>
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected object Entity
{ get; set; }
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
29
/// 每个Form都单独维护自己的一个Context,防止产生冲突或者引发错误
30
/// </summary>
31
protected StudentManagerDataContext Context
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
get
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
if (context == null) throw new ArgumentNullException();
36
return context;
37
}
38
}
39![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
41
/// 标识是编辑信息还是添加新信息,
42
/// </summary>
43![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected bool IsEdit
{ get
{ return isEidt; } }
44![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
public Form_Base()
46![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ }
47![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
public Form_Base(object entity)
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
50![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (entity != null)
{ isEidt = true; this.Entity = entity; }
51
//AppInfo包含了系统的配置信息
52
context = new StudentManagerDataContext(AppInfo.ConnectionString);
53
}
54![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
55
//判断当前操作的实体是否有效
56
protected bool ISLegal()
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
//全局信息,标识当前实体
59
return DataPool.StudentNumber != "0";
60
}
61![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
public void ShowHandleEntityError(string message)
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
72
/// 添加,修改学生信息(除基本信息)的逻辑,统一处理(多态的实现)
73
/// </summary>
74
protected void AddOrEditInfo()
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
try
77![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
78
if (ISLegal())
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
ConstructEntityInfo();
81
SMSApplication.ShowOperateSucceedResult();
82
}
83
else ShowHandleEntityError(null); ;
84
}
85
catch (FormatException exp)
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
//SMSApplication系统写入日志和显示友好信息的组件,XmlSelectorInfo记录xml配置文件寻址信息
88
SMSApplication.WriteLogAndShowSuggestiveInfo(exp.Message, SysConfig.Document.SelectNodes(XmlSelectorInfo.FormatError)[0].InnerText);
89
}
90
catch (OverflowException exo)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
SMSApplication.WriteLogAndShowSuggestiveInfo(exo.Message, SysConfig.Document.SelectNodes(XmlSelectorInfo.OverFlowError)[0].InnerText);
93
}
94
catch (Exception ex)
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
ShowHandleEntityError(ex.Message);
97
}
98
this.Dispose();
99
}
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
101
/// 获得新信息实体的方法,由子类实现
102
/// </summary>
103
protected virtual void ConstructEntityInfo()
104![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
105
throw new NotImplementedException();
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
}
109
}
110![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
其中几个子类的实现:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Windows.Forms;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
using StudentManagerV3.DataBase;
11![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
namespace StudentManagerV3
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
14
public partial class Form_Game : Form_Base
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
17
/// 修改添加时对应的实体
18
/// </summary>
19
private Game game = new Game();
20![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
public Form_Game(DataGridView view, object entity)
22
: base(entity)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
24
//这个通过Base放到父类中更合适,这样每个子类又减少了代码量,放到这里也算是错误代码的样例介绍把。呵呵
25
this.GridView = view;
26
this.Entity = entity;
27![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
InitializeComponent();
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
//如果是编辑,自然要初始化控件内容
31
if (IsEdit)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
private void button_AddGame_Click(object sender, EventArgs e)
41![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
42
//调用父类的逻辑
43
AddOrEditInfo();
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
46
/// 实现父类的方法
47
/// </summary>
48
protected override void ConstructEntityInfo()
49![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
game.StudentNumber = DataPool.StudentNumber;
58
Context.Games.InsertOnSubmit(game);
59
}
60
Context.SubmitChanges();
61
GridView.DataSource = GetIQueryable.GetGameQueryable(Context);
62
}
63![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
private void button_CancleGame_Click(object sender, EventArgs e)
65![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
this.Dispose();
67
}
68
}
69
}
70![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Linq;
3
using System.Windows.Forms;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
using StudentManagerV3.DataBase;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace StudentManagerV3
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
10
/// 注释没写,同上一个类差不多
11
/// </summary>
12
public partial class Form_Support : Form_Base
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
private StudentSupport support = new StudentSupport();
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
public Form_Support(DataGridView gridview, object entity)
17
: base(entity)
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
this.GridView = gridview;
20
this.Entity = entity;
21
InitializeComponent();
22
if (IsEdit)
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
private void button_AddSupport_Click(object sender, EventArgs e)
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
34
AddOrEditInfo();
35
}
36![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
protected override void ConstructEntityInfo()
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
support.StudentNumber = DataPool.StudentNumber;
48
Context.StudentSupports.InsertOnSubmit(support);
49
}
50
Context.SubmitChanges();
51
GridView.DataSource = GetIQueryable.GetSupportQueryable(Context);
52
}
53![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
private void button_CancleSupport_Click(object sender, EventArgs e)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
this.Dispose();
57
}
58
}
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
多态的实现基本就是子类中调用AddOrEditInfo()的过程了。呵呵 ,就到这里拉
原创文章,转载请注明出处!
All CopyRight Reserved !
主页:http://jingtao.cnblogs.com
QQ:307073463
Email:jingtaodeemail@qq.com
MSN:sunjingtao@live.com