stella forum 知识库-给sf v2.0创建一个新的数据操作层

在我提供下载的这个sf2中,提供了使用spl的数据操作层,下面我们一起来看如何给sf2提供一个新的数据操作层。

sf2使用的数据操作层必须要实现IDAL接口,因此一种办法就是一个类一个类的写下来,每个类都实现特定的接口,这样做的坏处是容易让人感到烦躁,因为要做的东西实在是太多了。我采用的办法就是一个功能一个功能的去做。这样做完一个功能就立刻见得到效果,有了成就感,速度就快多了。

我们要做的是可以让首页显示出来,为此应该去查看default.aspx都用到了哪些数据操作。不难看出,首页应该有的操作主要是获取全部板块的信息,还有论坛统计数据信息。在这里有一个好办法就是直接到解决方案中的SPLDAL中去查相关的类。
首先来看Forum类,该类实现IDAL.IForum接口,而实际上我们现在只需要具体化Forum类的GetDistricts和GetForumData这两个方法,而把接口契约中其他的方法先空着。因为在首页显示的时候并不会调用该接口的其他方法。比如说CreateUser方法,这个是在用户注册时需要调用的。
做完这个后,我的Forum类的全部代码如下:
using System;
using Stella2.DataControl.DataEntity;
using Stella2.DataControl.DataLogic;
using System.Data.SqlClient;
using System.Data;

namespace Stella2.CheagleDAL
{
 /// <summary>
 /// Forum 的摘要说明。
 /// </summary>
 public class Forum : IDAL.IForum
 {
  public Forum()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  #region IForum 成员

  public Stella2.Model.Districts GetDistricts()
  {
   DataControl.DataLogic.usp_DistrictByAll proc=new Stella2.DataControl.DataLogic.usp_DistrictByAll();
   SqlConnection conn=LogicTools.GetStella2Conn();
   try
   {
    conn.Open();
    DataTable dt=proc.ExecDataTable(conn);
    Model.Districts ds=new Stella2.Model.Districts();
    foreach(DataRow dr in dt.Rows)
    {
     Stella2.Model.District d=new Stella2.Model.District();
     d.Id=(int)dr["ID"];
     d.Name=(string)dr["NAME"];
     d.Description=(string)dr["DESCRIPTION"];
     d.Lv=(int)dr["LV"];

     ds.addDistrict(d);
    }
    return ds;
   }
   catch(Exception ex)
   {
    throw ex;
   }
   finally
   {
    conn.Close();
   }
   
  }

  public void RemoveDistrict(int DistrictId)
  {
   // TODO:  添加 Forum.RemoveDistrict 实现
  }

  public bool CreateUser(Stella2.Model.User user)
  {
   // TODO:  添加 Forum.CreateUser 实现
   return false;
  }

  public void RemoveUser(int UserId)
  {
   // TODO:  添加 Forum.RemoveUser 实现
  }

  public Stella2.Model.ForumData GetForumData()
  {
   DataControl.DataLogic.usp_ForumDataByAll proc=new usp_ForumDataByAll();
   SqlConnection conn=LogicTools.GetStella2Conn();
   try
   {
    conn.Open();
    DataTable dt=proc.ExecDataTable(conn);
    Stella2.Model.ForumData fd=new Stella2.Model.ForumData();
    foreach(DataRow dr in dt.Rows)
    {
     
     fd.ArtSum=(int)dr["ArtSum"];
     fd.LastRegUser=(string)dr["LastRegUser"];
     fd.TopicSum=(int)dr["TopicSum"];
     fd.UserSum=(int)dr["UserSum"];
     fd.ViewSum=(int)dr["ViewSum"];
     
    }
    return fd;
   }
   catch(Exception ex)
   {
    throw ex;
   }
   finally
   {
    conn.Close();
   }
  }

  public void CreateDistrict(Stella2.Model.District dist)
  {
   // TODO:  添加 Forum.CreateDistrict 实现
  }

  #endregion
 }
}
大家可能对我所使用的数据访问技术比较感兴趣,上面所使用的数据访问技术是现在我所在的公司内通用的一种方法,其底层是用SqlClient实现的,然后在外面看到的是对存储过程的包装,再具体的我就不说啦,因为我也没仔细看,感觉是用起来方便,但是每个存储过程都要包装,要不是有本技术的发明人(汗……)写的代码生成器,非累死不行。
这样一个一个的来,直到最后完成全部的数据操作功能!

故意不把类的方法具体化,刷新页面,根据错误提示找需要具体化的方法。
开发的时候,需要在web项目中添加对正开发的数据层的引用(虽然实际上并不需要这样引用,因为web根本不知道最底层的数据层是什么样子的),这样便于跟踪调试。

posted on 2005-07-15 17:14  Notus|南色的风  阅读(754)  评论(1编辑  收藏  举报