多态的应用

      最近在写学校的SRTP项目--学生信息管理系统,其中涉及到对以下信息的数据库操作。当然持久化之前要对数据的合法性进行验证,如果非法要提示合理信息,如果有其他问题会抛出异常。
         这些信息几乎按种来分类,每类一个数据表,也就是一类实体,除了各个实体属性不同其余操作就相差不大了。那么怎样来对这些信息进行统一的操作呢?这里就用到了多态。下面就用我的实现来作为例子吧,如果大家有什么好的想法可以提出来,不对的地方希望大家指出。谢谢,呵呵!
               涉及到信息:       
     1.  基本信息:学号,姓名,班级,专业,年龄,身份证号,籍贯等

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

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

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

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

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

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

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

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

ContractedBlock.gif ExpandedBlockStart.gif Code
  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.ComponentModel;
  4None.gifusing System.Data;
  5None.gifusing System.Drawing;
  6None.gifusing System.Linq;
  7None.gifusing System.Text;
  8None.gifusing System.Windows.Forms;
  9None.gif
 10None.gifusing StudentManagerV3.DataBase;
 11None.gif
 12None.gifnamespace StudentManagerV3
 13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 14InBlock.gif    public partial class Form_Base : Form
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 16InBlock.gif        private StudentManagerDataContext context;
 17InBlock.gif        private bool isEidt = false;
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 19InBlock.gif        /// 视图的引用,当模型发生改变时更新视图,呈献给用户新结果
 20ExpandedSubBlockEnd.gif        /// </summary> 

 21ExpandedSubBlockStart.gifContractedSubBlock.gif        protected DataGridView GridView dot.gifgetset; }
 22InBlock.gif
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 24InBlock.gif        /// 如果是编辑信息,实体不为空,强制转换成用户需要的实体
 25ExpandedSubBlockEnd.gif        /// </summary>

 26ExpandedSubBlockStart.gifContractedSubBlock.gif        protected object Entity dot.gifgetset; }
 27InBlock.gif
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 29InBlock.gif        /// 每个Form都单独维护自己的一个Context,防止产生冲突或者引发错误
 30ExpandedSubBlockEnd.gif        /// </summary>

 31InBlock.gif        protected StudentManagerDataContext Context
 32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 33InBlock.gif            get
 34ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 35InBlock.gif                if (context == nullthrow new ArgumentNullException();
 36InBlock.gif                return context;
 37ExpandedSubBlockEnd.gif            }

 38ExpandedSubBlockEnd.gif        }

 39InBlock.gif
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 41InBlock.gif        /// 标识是编辑信息还是添加新信息,
 42ExpandedSubBlockEnd.gif        /// </summary>

 43ExpandedSubBlockStart.gifContractedSubBlock.gif        protected bool IsEdit dot.gifget dot.gifreturn isEidt; } }
 44InBlock.gif
 45InBlock.gif        public Form_Base()
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{ }
 47InBlock.gif
 48InBlock.gif        public Form_Base(object entity)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            if (entity != nulldot.gif{ isEidt = truethis.Entity = entity; }
 51InBlock.gif            //AppInfo包含了系统的配置信息
 52InBlock.gif            context = new StudentManagerDataContext(AppInfo.ConnectionString);
 53ExpandedSubBlockEnd.gif        }

 54InBlock.gif
 55InBlock.gif        //判断当前操作的实体是否有效
 56InBlock.gif        protected bool ISLegal()
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58InBlock.gif            //全局信息,标识当前实体
 59InBlock.gif            return DataPool.StudentNumber != "0";
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif
 62InBlock.gif        public void ShowHandleEntityError(string message)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 64InBlock.gif            if (message != null)
 65InBlock.gif                SMSApplication.WriteLogAndShowSuggestiveInfo(message,
 66InBlock.gif                    SysConfig.Document.SelectNodes(XmlSelectorInfo.ApplicationError)[0].InnerText);
 67InBlock.gif            else SMSApplication.WriteLogAndShowSuggestiveInfo(message,
 68InBlock.gif                SysConfig.Document.SelectNodes(XmlSelectorInfo.EntityError)[0].InnerText);
 69ExpandedSubBlockEnd.gif        }

 70InBlock.gif
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 72InBlock.gif        /// 添加,修改学生信息(除基本信息)的逻辑,统一处理(多态的实现)
 73ExpandedSubBlockEnd.gif        /// </summary>

 74InBlock.gif        protected void AddOrEditInfo()
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            try
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                if (ISLegal())
 79ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 80InBlock.gif                    ConstructEntityInfo();
 81InBlock.gif                    SMSApplication.ShowOperateSucceedResult();
 82ExpandedSubBlockEnd.gif                }

 83InBlock.gif                else ShowHandleEntityError(null); ;
 84ExpandedSubBlockEnd.gif            }

 85InBlock.gif            catch (FormatException exp)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 87InBlock.gif                //SMSApplication系统写入日志和显示友好信息的组件,XmlSelectorInfo记录xml配置文件寻址信息
 88InBlock.gif                SMSApplication.WriteLogAndShowSuggestiveInfo(exp.Message, SysConfig.Document.SelectNodes(XmlSelectorInfo.FormatError)[0].InnerText);
 89ExpandedSubBlockEnd.gif            }

 90InBlock.gif            catch (OverflowException exo)
 91ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 92InBlock.gif                SMSApplication.WriteLogAndShowSuggestiveInfo(exo.Message, SysConfig.Document.SelectNodes(XmlSelectorInfo.OverFlowError)[0].InnerText);
 93ExpandedSubBlockEnd.gif            }

 94InBlock.gif            catch (Exception ex)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 96InBlock.gif                ShowHandleEntityError(ex.Message);
 97ExpandedSubBlockEnd.gif            }

 98InBlock.gif            this.Dispose();
 99ExpandedSubBlockEnd.gif        }

100ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
101InBlock.gif        /// 获得新信息实体的方法,由子类实现
102ExpandedSubBlockEnd.gif        /// </summary>

103InBlock.gif        protected virtual void ConstructEntityInfo()
104ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
105InBlock.gif            throw new NotImplementedException();
106ExpandedSubBlockEnd.gif        }

107InBlock.gif
108ExpandedSubBlockEnd.gif    }

109ExpandedBlockEnd.gif}

110None.gif

其中几个子类的实现:

ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.ComponentModel;
 4None.gifusing System.Data;
 5None.gifusing System.Drawing;
 6None.gifusing System.Linq;
 7None.gifusing System.Text;
 8None.gifusing System.Windows.Forms;
 9None.gif
10None.gifusing StudentManagerV3.DataBase;
11None.gif
12None.gifnamespace StudentManagerV3
13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
14InBlock.gif    public partial class Form_Game : Form_Base
15ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
17InBlock.gif        /// 修改添加时对应的实体
18ExpandedSubBlockEnd.gif        /// </summary>

19InBlock.gif        private Game game = new Game();
20InBlock.gif
21InBlock.gif        public Form_Game(DataGridView view, object entity)
22InBlock.gif            : base(entity)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            //这个通过Base放到父类中更合适,这样每个子类又减少了代码量,放到这里也算是错误代码的样例介绍把。呵呵
25InBlock.gif            this.GridView = view;
26InBlock.gif            this.Entity = entity;
27InBlock.gif
28InBlock.gif            InitializeComponent();
29InBlock.gif
30InBlock.gif            //如果是编辑,自然要初始化控件内容
31InBlock.gif            if (IsEdit)
32ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
33InBlock.gif                game = entity as Game;
34InBlock.gif                dateTimePicker_GameTime.Text = game.GameDateTime.ToString();
35InBlock.gif                textBox_GameName.Text = game.GameName;
36InBlock.gif                textBox_GameResult.Text = game.GameResult;
37ExpandedSubBlockEnd.gif            }

38ExpandedSubBlockEnd.gif        }

39InBlock.gif
40InBlock.gif        private void button_AddGame_Click(object sender, EventArgs e)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            //调用父类的逻辑
43InBlock.gif            AddOrEditInfo();
44ExpandedSubBlockEnd.gif        }

45ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
46InBlock.gif        /// 实现父类的方法
47ExpandedSubBlockEnd.gif        /// </summary>

48InBlock.gif        protected override void ConstructEntityInfo()
49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
50InBlock.gif            if (IsEdit)
51InBlock.gif                game = (from ga in Context.Games where ga.GameID == ((Game)Entity).GameID select ga).Single();
52InBlock.gif            game.GameResult = textBox_GameResult.Text;
53InBlock.gif            game.GameName = textBox_GameName.Text;
54InBlock.gif            game.GameDateTime = Convert.ToDateTime(dateTimePicker_GameTime.Text);
55InBlock.gif            if (!IsEdit)
56ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
57InBlock.gif                game.StudentNumber = DataPool.StudentNumber;
58InBlock.gif                Context.Games.InsertOnSubmit(game);
59ExpandedSubBlockEnd.gif            }

60InBlock.gif            Context.SubmitChanges();
61InBlock.gif            GridView.DataSource = GetIQueryable.GetGameQueryable(Context);
62ExpandedSubBlockEnd.gif        }

63InBlock.gif
64InBlock.gif        private void button_CancleGame_Click(object sender, EventArgs e)
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            this.Dispose();
67ExpandedSubBlockEnd.gif        }

68ExpandedSubBlockEnd.gif    }

69ExpandedBlockEnd.gif}

70None.gif
ContractedBlock.gif ExpandedBlockStart.gif Code
 1None.gifusing System;
 2None.gifusing System.Linq;
 3None.gifusing System.Windows.Forms;
 4None.gif
 5None.gifusing StudentManagerV3.DataBase;
 6None.gif
 7None.gifnamespace StudentManagerV3
 8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
10InBlock.gif    /// 注释没写,同上一个类差不多
11ExpandedSubBlockEnd.gif    /// </summary>

12InBlock.gif    public partial class Form_Support : Form_Base
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        private StudentSupport support = new StudentSupport();
15InBlock.gif
16InBlock.gif        public Form_Support(DataGridView gridview, object entity)
17InBlock.gif            : base(entity)
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            this.GridView = gridview;
20InBlock.gif            this.Entity = entity;
21InBlock.gif            InitializeComponent();
22InBlock.gif            if (IsEdit)
23ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
24InBlock.gif                support = entity as StudentSupport;
25InBlock.gif                dateTimePicker_SupportTime.Text = support.SupportTime.ToShortDateString();
26InBlock.gif                textBox_SupportAmount.Text = support.SupportAmount.ToString();
27InBlock.gif                textBox_SupportRemark.Text = support.SupportRemark;
28InBlock.gif                textBox_SupportSource.Text = support.SupportFrom;
29ExpandedSubBlockEnd.gif            }

30ExpandedSubBlockEnd.gif        }

31InBlock.gif
32InBlock.gif        private void button_AddSupport_Click(object sender, EventArgs e)
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            AddOrEditInfo();
35ExpandedSubBlockEnd.gif        }

36InBlock.gif
37InBlock.gif        protected override void ConstructEntityInfo()
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
39InBlock.gif            if (IsEdit)
40InBlock.gif                support = (from su in Context.StudentSupports where su.SupportID == ((StudentSupport)Entity).SupportID select su).Single();
41InBlock.gif            support.SupportAmount = Convert.ToInt32(textBox_SupportAmount.Text);
42InBlock.gif            support.SupportFrom = textBox_SupportSource.Text;
43InBlock.gif            support.SupportRemark = textBox_SupportRemark.Text;
44InBlock.gif            support.SupportTime = Convert.ToDateTime(dateTimePicker_SupportTime.Text);
45InBlock.gif            if (!IsEdit)
46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
47InBlock.gif                support.StudentNumber = DataPool.StudentNumber;
48InBlock.gif                Context.StudentSupports.InsertOnSubmit(support);
49ExpandedSubBlockEnd.gif            }

50InBlock.gif            Context.SubmitChanges();
51InBlock.gif            GridView.DataSource = GetIQueryable.GetSupportQueryable(Context);
52ExpandedSubBlockEnd.gif        }

53InBlock.gif
54InBlock.gif        private void button_CancleSupport_Click(object sender, EventArgs e)
55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
56InBlock.gif            this.Dispose();
57ExpandedSubBlockEnd.gif        }

58ExpandedSubBlockEnd.gif    }

59ExpandedBlockEnd.gif}

60None.gif

多态的实现基本就是子类中调用AddOrEditInfo()的过程了。呵呵 ,就到这里拉

 

原创文章,转载请注明出处!
All CopyRight Reserved !

 

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

QQ:307073463
Email:jingtaodeemail@qq.com
MSN:sunjingtao@live.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值