淘特Asp.NetCms 二次开发说明
查看全部词条命名空间:Main
导航: 上一页
CMS框架说明及特色:
- 基于MVC三层架构,业务、控制、显示层层分离,面向对象的编程思想,扩展开发容易。
- 数据库模块统一放置于DAO中,所有业务逻辑相关操作,由统一的ObjectFactory获取并实例化,便于系统的统一管理。
- 统一的数据库BEAN工厂(DataField),通过引用(import)系统中的DataField类,即可完成数据库中字段的复值、取值。省去了为每个表写单独的实例化信息类了的繁锁。
- 系统集成Lucene.net高效索引、搜索引擎,该模块是先将数据库内容采用一定的分词技术生成索引,前台模糊查询时读取的索引文件,因而速度非常快,同时检索的同时不在查询数据库,从而节省了大量资源。
- 大量采用了Ajax技术,后台无限级分类、WEB采集等模块均加入了Ajax技术异步调用,极大优化了程序代码,真正意义的实现了“无限”级数量级业务。
TotNetCms提供了完整的数据库访问框架,所以在CMS中二次开发只需要引用DB工具类就可以了,它存在TotCms.FrameWork.Db中。下面以TotNetCms中的广告管理类做为说明。
事例:广告管理类的设计
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using TotCms.FrameWork.Db;
using TotCms.Web.Admin.UI;
using TotCms.FrameWork.Util;
namespace TotCms.FrameWork.Dao
{
public class AdDao
{
public int GetLastId()
{
object id;
SqlCommand cmd = DbUtil.GetCommand("select top 1 id from t_ad order by id desc", true);
id = DbUtil.Instance.ExecuteScalar(cmd);
return Convert.ToInt32(id);
}
public void Add(int categoryid, string title, string content,int adtype, DateTime expirestime, DateTime moditime)
{
int id = GetLastId()+1;
SqlCommand cmd = DbUtil.GetCommand("insert into t_ad(id,CategoryId,Title,Content,AdType,ExpiresTime,ModiTime)
values(@id,@CategoryId,@Title,@Content,@AdType,@ExpiresTime,@ModiTime)", true);
cmd.Parameters.Add(DataHelper.MakeInParam("@id", SqlDbType.Int, 0, id));
cmd.Parameters.Add(DataHelper.MakeInParam("@CategoryId", SqlDbType.Int, 0, categoryid));
cmd.Parameters.Add(DataHelper.MakeInParam("@Title", SqlDbType.VarChar, 0, title));
cmd.Parameters.Add(DataHelper.MakeInParam("@Content", SqlDbType.Text, 0, content));
cmd.Parameters.Add(DataHelper.MakeInParam("@AdType", SqlDbType.Int, 0, adtype));
cmd.Parameters.Add(DataHelper.MakeInParam("@ExpiresTime", SqlDbType.DateTime, 0, expirestime));
cmd.Parameters.Add(DataHelper.MakeInParam("@ModiTime", SqlDbType.DateTime, 0, moditime));
DbUtil.Instance.ExecuteNonQuery(cmd);
}
public void Mod(int id, int categoryid, string title, string content, int adtype, DateTime expirestime, DateTime moditime)
{
SqlCommand cmd = DbUtil.GetCommand("update t_ad set CategoryId=@CategoryId,Title=@Title,Content=@Content,
AdType=@AdType,ExpiresTime=@ExpiresTime,
ModiTime=@ModiTime where id=@id", true);
cmd.Parameters.Add(DataHelper.MakeInParam("@CategoryId", SqlDbType.Int, 0, categoryid));
cmd.Parameters.Add(DataHelper.MakeInParam("@Title", SqlDbType.VarChar, 0, title));
cmd.Parameters.Add(DataHelper.MakeInParam("@Content", SqlDbType.Text, 0, content));
cmd.Parameters.Add(DataHelper.MakeInParam("@AdType", SqlDbType.Int, 0, adtype));
cmd.Parameters.Add(DataHelper.MakeInParam("@ExpiresTime", SqlDbType.DateTime, 0, expirestime));
cmd.Parameters.Add(DataHelper.MakeInParam("@ModiTime", SqlDbType.DateTime, 0, moditime));
cmd.Parameters.Add(DataHelper.MakeInParam("@id", SqlDbType.Int, 0, id));
DbUtil.Instance.ExecuteNonQuery(cmd);
}
public void Delete(int id)
{
SqlCommand cmd = DbUtil.GetCommand("delete from t_ad where id=@id", true);
cmd.Parameters.Add(DataHelper.MakeInParam("@id", SqlDbType.Int, 0, id));
DbUtil.Instance.ExecuteNonQuery(cmd);
}
public DataField Get(int id)
{
SqlCommand cmd = DbUtil.GetCommand("select CategoryId,Title,Content,AdType,ExpiresTime,
ModiTime from t_ad where id=@id", true);
cmd.Parameters.Add(DataHelper.MakeInParam("@id", SqlDbType.Int, 0, id));
return (DbUtil.Instance.GetFirstData(cmd, "CategoryId,Title,Content,AdType,ExpiresTime,ModiTime"));
}
public List<DataField> GetList(int categoryid)
{
string fields = "id,CategoryId,Title,Content,AdType,ExpiresTime,ModiTime";
StringBuilder sql = new StringBuilder(512);
sql.Append("select ");
sql.Append(fields);
sql.Append(" from t_ad where 1=1");
if (categoryid > 0)
{
sql.Append(" and CategoryId=").Append(categoryid);
}
sql.Append(" order by id desc");
SqlCommand cmd = DbUtil.GetCommand(sql.ToString(), true);
return DbUtil.Instance.GetList(cmd, fields);
}
public DataTable GetDataTable(int categoryid, string keys, int currentpage, int pagesize)
{
string fields = "id,CategoryId,Title,Content,AdType,ExpiresTime,ModiTime";
StringBuilder sql = new StringBuilder(512);
sql.Append("select top ");
sql.Append(pagesize + " ");
sql.Append(fields);
sql.Append(" from t_ad WHERE (id <=(SELECT MIN(id) FROM (SELECT TOP " + ((currentpage - 1) * pagesize + 1)
+ " id FROM t_ad where 1=1");
if (categoryid > 0)
{
sql.Append(" and CategoryId=").Append(categoryid);
}
if (!string.IsNullOrEmpty(keys))
{
sql.Append(" and Title like '%").Append(keys).Append("%'");
}
sql.Append(" ORDER BY id DESC) AS t))");
if (categoryid > 0)
{
sql.Append(" and CategoryId=").Append(categoryid);
}
if (!string.IsNullOrEmpty(keys))
{
sql.Append(" and Title like '%").Append(keys).Append("%'");
}
sql.Append(" ORDER BY id DESC");
LogUtil.Debug(sql.ToString());
SqlCommand cmd = DbUtil.GetCommand(sql.ToString(), true);
return DbUtil.Instance.GetData(cmd);
}
public double GetCount(int categoryid, string keys)
{
double ret = 0D;
StringBuilder sql = new StringBuilder(512);
sql.Append("select count(*) from t_ad where 1=1");
if (categoryid > 0)
{
sql.Append(" and CategoryId=").Append(categoryid);
}
if (!string.IsNullOrEmpty(keys))
{
sql.Append(" and Title like '%").Append(keys).Append("%'");
}
SqlCommand cmd = DbUtil.GetCommand(sql.ToString(), true);
ret = Convert.ToDouble(DbUtil.Instance.ExecuteScalar(cmd));
return ret;
}
public string GetContent(int id)
{
SqlCommand cmd = DbUtil.GetCommand("select Content from t_ad where id=@id", true);
cmd.Parameters.Add(DataHelper.MakeInParam("@id", SqlDbType.Int, 0, id));
string p = (string)DbUtil.Instance.GetField(cmd);
return p;
}
}
}
以上是广告管理的全部代码,下面重要介绍两个函数:
- GetList函数:此函数返回的是List<DataField>,DataField也就是我们CMS中提供一个数据库字段操作类,通过它可以访问返回的数据库每行的字段值。比如前台可以这么来取数据:
AdDao ad=new AdDao();
List<DataField> list=ad.GetList();
Foreach(DataField df in list){
Response.Write(df.GetFieldValue(“id”));// GetFieldValue函数中参数即为表t_ad中的字段名称
Response.Write(df.GetFieldValue(“Title”));
}
- GetDataTable函数:此函数返回DataTable对象,DataTable经常用到asp.net数据控件中(如Repeater,GridView),因此在asp.net页面中可以直接通过此函数将数据控制绑定数据。以下是广告管理页面中的绑定代码(为了演示方便,省略了一部分无关代码):
Admanage.aspx.cs页面
protected void Page_Load(object sender, EventArgs e)
{
AdDao ad=new AdDao();
this.RepeateList.DataSource = ad.GetDataTable(0, “”, 1, 10);
}
上面代码中,只需要调用AdDao中的GetDataTable就完成了RepeateList绑定。具体的业务逻辑交由实体数据Dao类来管理,从业务层次上比较分明,利于项目的扩展和管理。
