路来,而不从面向对象的角度考虑,造成业务逻辑与页面HTML代码混杂在一起,一旦页面原型改变,相应
的程序也要修改,这样造成代码的可重用性太低。而ASP.NET或JSP比asp最大的一个进步就是面向对象,
使代码可重用性达到最高。作为一个典型的web程序来说,一般把它分为三层比较理想,业务层,数据层
和页面显示层。下面以一个舆论调查的例子来讲一下。
让我们先来看一下如果一个舆论调查从面向对象的角度来考虑应该是怎样的。首先,从数据方面来说
,一个舆论调查应该有一个调查主题,然后应该有几个调查项,最后还应该有参加调查的用户以及比如调
查起止时间等等;其次,从舆论调查的方法来说,很简单,一个投票的方法,然后就是显示调查结果的方
法。知道了上面这些,我们就可以这样来构造一个调查类:
namespace MyClass.Util
{
using System;
using System.Collections ;
using System.Drawing ;
using MyClass.Util ;
///
/// 一个通用的调查类
///
public class Survey : object
{
///
/// 调查编号
///
///
/// 在数据库中是varchar型,20字节
///
protected string m_strID ;
///
/// 调查标题
///
protected string m_strTitle ;
///
/// 调查开始时间
///
protected DateTime m_datBeginTime ;
///
/// 调查截止时间
///
protected DateTime m_datEndTime ;
///
/// 点击数
///
///
/// 浏览人数
///
protected int m_intHits ;
///
/// 调查项
///
protected ArrayList m_arrItems ;
//属性
///
/// 调查标题
///
public string Title
{
get
{
return m_strTitle ;
}
set
{
m_strTitle = value ;
}
}
///
/// 总共点击数
///
public int Hits
{
get
{
return m_intHits ;
}
set
{
m_intHits = 0 ;
}
}
///
/// 调查开始时间属性
///
public DateTime BeginTime
{
get
{
return m_datBeginTime ;
}
set
{
m_datBeginTime = value ;
}
}
///
/// 调查截止时间属性
///
public DateTime EndTime
{
get
{
return m_datEndTime ;
}
set
{
m_datEndTime = value ;
}
}
///
/// 调查项集合
///
///
public ArrayList Items
{
get
{
return m_arrItems ;
}
set
{
m_arrItems = value ;
}
}
///
/// 调查编号
///
public string SurveyID
{
get
{
return m_strID ;
}
set
{
m_strID = value ;
}
}
///
/// 构造函数
///
public Survey()
{
//
// TODO: Add Constructor Logic here
//
m_strTitle = "" ;
m_arrItems = new ArrayList() ;
}
///
/// 重载构造函数
///
/// 调查标题
///
public Survey(string a_strTitle)
{
m_strTitle = a_strTitle ;
m_datBeginTime = DateTime.Today ;
m_datEndTime = DateTime.Today ;
m_arrItems = new ArrayList() ;
}
///
/// 重载构造函数
///
/// 调查标题
/// 开始时间
/// 结束时间
///
public Survey(string a_strTitle , DateTime a_datBeginTime , DateTime
a_datEndTime)
{
m_strTitle = a_strTitle ;
m_datBeginTime = a_datBeginTime ;
m_datEndTime = a_datEndTime ;
}
///
/// 增加调查项
///
///
public void AddItem(object a_objItem)
{
if (a_objItem is SurveyItem)
{
m_arrItems.Add(a_objItem) ;
}
}
///
/// 虚函数
///
/// 该项编号
public virtual void Vote(int a_intID)
{
}
///
/// 虚函数,保存到数据库
///
///
public virtual void SaveItem(object a_objItem)
{
}
///
/// 虚函数,保存调查到数据库
///
public virtual void SaveToDatabase(string m_strSurveyID)
{
throw(new Exception("基类函数不能直接使用")) ;
}
///
/// 从数据库中取得调查及调查项
///
/// 对应数据库中的id
public virtual void LoadFromDatabase(string a_strID)
{
}
///
/// 查找调查项
///
/// 调查项编号
public SurveyItem GetItem(int a_intID)
{
if (a_intID > 0 && a_intID < m_arrItems.Count)
{
return (SurveyItem)(m_arrItems[a_intID]) ;
}
else
{
throw(new Exception("调查项下标越界")) ;
}
}
///
/// 查找调查项
///
/// 调查项标题
///
public int IndexOf(string a_strText)
{
for (int i = 0 ; i < m_arrItems.Count ; i ++)
{
if (a_strText == ((SurveyItem)m_arrItems[i]).Text)
{
return i ;
}
}
return -1 ;
}
///
/// 查找调查项
///
/// 调查项编号
///
/// 根据调查项标号来查找调查项,如果找到返回序号,否则返回-1
public int IndexOf(int a_intID)
{
for ( int i = 0 ; i < m_arrItems.Count ; i ++)
{
if (a_intID == ((SurveyItem)m_arrItems[i]).ID)
{
return i ;
}
}
return - 1 ;
}
///
/// 显示结果
///
/// 生成图片的保存位置
public void CreateResultImage(string a_strFileName , MyChart.ChartType
a_intChartType ,
int a_intWidth , int
a_intHeight , Color a_objBackColor)
{
//定义一个颜色数组
ArrayList ItemColors = new ArrayList() ;
ItemColors.Add(Color.Red) ;
ItemColors.Add(Color.Black) ;
ItemColors.Add(Color.Blue) ;
ItemColors.Add(Color.DeepSkyBlue) ;
ItemColors.Add(Color.Firebrick) ;
ItemColors.Add(Color.Orange) ;
ItemColors.Add(Color.Green) ;
ItemColors.Add(Color.WhiteSmoke) ;
ItemColors.Add(Color.Tan) ;
ItemColors.Add(Color.DarkSeaGreen) ;
ItemColors.Add(Color.Fuchsia) ;
ItemColors.Add(Color.Goldenrod) ;
MyChart myChart = new MyChart(m_strTitle , a_objBackColor ,
a_intWidth ,
a_intHeight , a_intChartType
, "人次") ;
for (int i = 0 ; i < m_arrItems.Count ; i++)
{
//调查项
SurveyItem item = (SurveyItem)m_arrItems[i] ;
//图表项
myChart.AddItem(new ChartItem(item.Text , item.Count ,
(Color)ItemColors[i])) ;
}
try
{
myChart.Create(a_strFileName) ;
}
catch(Exception e)
{
throw(new Exception(e.ToString())) ;
}
}
}
///
/// 调查项类
///
public class SurveyItem : object
{
///
/// 调查项编号,对应将来数据库中的id
///
protected int m_intID ;
///
/// 调查项标题
///
protected string m_strText ;
///
/// 调查项描述
///
protected string m_strDescription ;
///
/// 调查项数量
///
protected int m_intCount ;
public int ID
{
get
{
return m_intID ;
}
set
{
m_intID = value ;
}
}
///
/// 构造函数
///
public SurveyItem()
{
m_strText = "" ;
m_strDescription = "" ;
m_intCount = 0 ;
}
//属性
///
/// 调查项标题属性
///
public string Text
{
get
{
return m_strText ;
}
set
{
m_strText = value ;
}
}
///
/// 调查项描述属性
///
public string Description
{
get
{
return m_strDescription ;
}
set
{
m_strDescription = value ;
}
}
///
/// 调查项数量属性
///
public int Count
{
get
{
return m_intCount ;
}
set
{
m_intCount = value ;
}
}
//函数
///
/// 重载构造函数
///
/// 调查项编号
/// 调查项标题
/// 调查项描述
/// 调查项数量
public SurveyItem(int a_intID , string a_strText ,
string a_strDescription , int a_intCount)
{
m_intID = a_intID ;
m_strText = a_strText ;
m_strDescription = a_strDescription ;
m_intCount = a_intCount ;
}
public SurveyItem(string a_strText)
{
m_strText = a_strText ;
}
}
}
一个基本的调查类就做好了,这个可以作为业务层。但是你可以发现实际上这个类现在什么都做不了,原
因很简单,因为没有同数据库挂接。众所周知,没有数据库的支持什么都是白搭,那么,我们现在如何来
挂接数据库,也就是做数据层呢?
