多态的应用

添加人:iyond七级(4369分)   添加时间:2008-04-23    阅读次数:575  收藏此教程

最近在写学校的SRTP项目--学生信息管理系统,其中涉及到对以下信息的数据库操作。当然持久化之前要对数据的合法性进行验证,如果非法要提示合理信息,如果有其他问题会抛出异常。
这些信息几乎按种来分类,每类一个数据表,也就是一类实体,除了各个实体属性不同其余操作就相差不大了。那么怎样来对这些信息进行统一的操作呢?这里就用到了多态。下面就用我的实现来作为例子吧,如果大家有什么好的想法可以提出来,不对的地方希望大家指出。谢谢,呵呵!
涉及到信息:      
1.  基本信息:学号,姓名,班级,专业,年龄,身份证号,籍贯等
2.  家庭信息:家庭住址,家庭电话,家庭成员信息等
3.  奖学金信息:包括获得奖学金的数额,时间,项目等
4.  活动信息:参加的活动,活动举行的时间,活动的结果等
5.  资助信息:资助的项目,资助金额,资助时间等
6.  处分信息:处分的时间,原因,是否被撤销等
7.  素质测评信息:包括测评的时间,文体,时间等的得分情况
8.  参加的比赛信息:包括比赛名称,时间,结果等
实现概括:
每类信息的添加,修改都有一个专门的VIEW,也就是一个WinForm,他们共同继承Form_base,base处理共同问题,具体问题子类各自处理
From_Base实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using StudentManagerV3.DataBase;

namespace StudentManagerV3
{
    public partial class Form_Base : Form
    {
        private StudentManagerDataContext context;
        private bool isEidt = false;
        /**////


        /// 视图的引用,当模型发生改变时更新视图,呈献给用户新结果
        ///
        protected DataGridView GridView { get; set; }

        /**////


        /// 如果是编辑信息,实体不为空,强制转换成用户需要的实体
        ///
        protected object Entity { get; set; }

        /**////


        /// 每个Form都单独维护自己的一个Context,防止产生冲突或者引发错误
        ///
        protected StudentManagerDataContext Context
        {
            get
            {
                if (context == null) throw new ArgumentNullException();
                return context;
            }
        }

        /**////


        /// 标识是编辑信息还是添加新信息,
        ///
        protected bool IsEdit { get { return isEidt; } }

        public Form_Base()
        { }

        public Form_Base(object entity)
        {
            if (entity != null) { isEidt = true; this.Entity = entity; }
            //AppInfo包含了系统的配置信息
            context = new StudentManagerDataContext(AppInfo.ConnectionString);
        }

        //判断当前操作的实体是否有效
        protected bool ISLegal()
        {
            //全局信息,标识当前实体
            return DataPool.StudentNumber != "0";
        }

        public void ShowHandleEntityError(string message)
        {
            if (message != null)
                SMSApplication.WriteLogAndShowSuggestiveInfo(message,
                    SysConfig.Document.SelectNodes(XmlSelectorInfo.ApplicationError)[0].InnerText);
            else SMSApplication.WriteLogAndShowSuggestiveInfo(message,
                SysConfig.Document.SelectNodes(XmlSelectorInfo.EntityError)[0].InnerText);
        }

        /**////


        /// 添加,修改学生信息(除基本信息)的逻辑,统一处理(多态的实现)
        ///
        protected void AddOrEditInfo()
        {
            try
            {
                if (ISLegal())
                {
                    ConstructEntityInfo();
                    SMSApplication.ShowOperateSucceedResult();
                }
                else ShowHandleEntityError(null); ;
            }
            catch (FormatException exp)
            {
                //SMSApplication系统写入日志和显示友好信息的组件,XmlSelectorInfo记录xml配置文件寻址信息
                SMSApplication.WriteLogAndShowSuggestiveInfo(exp.Message, SysConfig.Document.SelectNodes(XmlSelectorInfo.FormatError)[0].InnerText);
            }
            catch (OverflowException exo)
            {
                SMSApplication.WriteLogAndShowSuggestiveInfo(exo.Message, SysConfig.Document.SelectNodes(XmlSelectorInfo.OverFlowError)[0].InnerText);
            }
            catch (Exception ex)
            {
                ShowHandleEntityError(ex.Message);
            }
            this.Dispose();
        }
        /**////
        /// 获得新信息实体的方法,由子类实现
        ///
        protected virtual void ConstructEntityInfo()
        {
            throw new NotImplementedException();
        }

    }
}

其中几个子类的实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using StudentManagerV3.DataBase;

namespace StudentManagerV3
{
    public partial class Form_Game : Form_Base
    {
        /**////


        /// 修改添加时对应的实体
        ///
        private Game game = new Game();

        public Form_Game(DataGridView view, object entity)
            : base(entity)
        {
            //这个通过Base放到父类中更合适,这样每个子类又减少了代码量,放到这里也算是错误代码的样例介绍把。呵呵
            this.GridView = view;
            this.Entity = entity;

            InitializeComponent();

            //如果是编辑,自然要初始化控件内容
            if (IsEdit)
            {
                game = entity as Game;
                dateTimePicker_GameTime.Text = game.GameDateTime.ToString();
                textBox_GameName.Text = game.GameName;
                textBox_GameResult.Text = game.GameResult;
            }
        }

        private void button_AddGame_Click(object sender, EventArgs e)
        {
            //调用父类的逻辑
            AddOrEditInfo();
        }
        /**////


        /// 实现父类的方法
        ///
        protected override void ConstructEntityInfo()
        {
            if (IsEdit)
                game = (from ga in Context.Games where ga.GameID == ((Game)Entity).GameID select ga).Single();
            game.GameResult = textBox_GameResult.Text;
            game.GameName = textBox_GameName.Text;
            game.GameDateTime = Convert.ToDateTime(dateTimePicker_GameTime.Text);
            if (!IsEdit)
            {
                game.StudentNumber = DataPool.StudentNumber;
                Context.Games.InsertOnSubmit(game);
            }
            Context.SubmitChanges();
            GridView.DataSource = GetIQueryable.GetGameQueryable(Context);
        }

        private void button_CancleGame_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
using System;
using System.Linq;
using System.Windows.Forms;

using StudentManagerV3.DataBase;

namespace StudentManagerV3
{
    /**////


    /// 注释没写,同上一个类差不多
    ///
    public partial class Form_Support : Form_Base
    {
        private StudentSupport support = new StudentSupport();

        public Form_Support(DataGridView gridview, object entity)
            : base(entity)
        {
            this.GridView = gridview;
            this.Entity = entity;
            InitializeComponent();
            if (IsEdit)
            {
                support = entity as StudentSupport;
                dateTimePicker_SupportTime.Text = support.SupportTime.ToShortDateString();
                textBox_SupportAmount.Text = support.SupportAmount.ToString();
                textBox_SupportRemark.Text = support.SupportRemark;
                textBox_SupportSource.Text = support.SupportFrom;
            }
        }

        private void button_AddSupport_Click(object sender, EventArgs e)
        {
            AddOrEditInfo();
        }

        protected override void ConstructEntityInfo()
        {
            if (IsEdit)
                support = (from su in Context.StudentSupports where su.SupportID == ((StudentSupport)Entity).SupportID select su).Single();
            support.SupportAmount = Convert.ToInt32(textBox_SupportAmount.Text);
            support.SupportFrom = textBox_SupportSource.Text;
            support.SupportRemark = textBox_SupportRemark.Text;
            support.SupportTime = Convert.ToDateTime(dateTimePicker_SupportTime.Text);
            if (!IsEdit)
            {
                support.StudentNumber = DataPool.StudentNumber;
                Context.StudentSupports.InsertOnSubmit(support);
            }
            Context.SubmitChanges();
            GridView.DataSource = GetIQueryable.GetSupportQueryable(Context);
        }

        private void button_CancleSupport_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
 

来源:http://www.cnblogs.com/jingtao/archive/2008/04/23/1167006.html

1页 第1上一页1下一页
相关的教程: 多态
收藏此教程

当前平均分: 0.0(2 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: