下面就是实际调用的程序原文件
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;
using clubstar.IPlug;
using clubstar.IPlug.DataOp;
using clubstar.uc;
using clubstar.DataOp;
namespace clubstar
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class InsertUser : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
public RandomNum SessionID;
private void Page_Load(object sender, System.EventArgs e)
{
Common dai=new Common();
//session("verifycode")=rndnum
//Response.Write(SessionID.Text.ToString());
this.Button1.Click += new System.EventHandler(OnPluginClick);
}
public delegate void PerformInsert(string plugname,StringBuilder sql);
private void OnPluginClick(object sender,EventArgs args)
{
StringBuilder sql=new StringBuilder();
sql.Append("insert into clubuser (ID,sessionID,username) values (2,'123123','fuck')");
Common InsertUser=new Common();
PerformInsert Insert=new PerformInsert(InsertUser.PerformInsert);
Insert("clubstar.IPlug.DataOp.User",sql); //调用插件所代理的方法
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button2_Click(object sender, System.EventArgs e)
{
}
}
}
而上面文件中的PerformInsert代理针对如下事件函数进行操作
using System;
using System.Configuration;
using clubstar.IPlug;
using System.Text;
using System.Data.OleDb;
namespace clubstar
{
/// <summary>
/// Common 的摘要说明。
/// </summary>
public class Common
{
public Common()
{
}
private PluginCollection m_plugins = null; //被回调用于对插件进行插入操作
public void PerformInsert(string plugname,StringBuilder sql) //执行插入操作
{
m_plugins = (PluginCollection )ConfigurationSettings.GetConfig("IPlugs");
PlugSqlOp context = new PlugSqlOp(sql.ToString());
foreach(IPlugStarClub plugin in m_plugins)
{
if(plugin.Name==plugname)
{
plugin.PerformInsert(context);
return ;
}
}
}
public void PerformDelete(string plugname,StringBuilder sql)//执行删除操作
{
m_plugins = (PluginCollection )ConfigurationSettings.GetConfig("IPlugs");
PlugSqlOp context = new PlugSqlOp(sql.ToString());
foreach(IPlugStarClub plugin in m_plugins)
{
if(plugin.Name==plugname)
{
plugin.PerformDelete(context);
return ;
}
}
}
public void PerformUpdate(string plugname,StringBuilder sql)//执行更新操作
{
m_plugins = (PluginCollection )ConfigurationSettings.GetConfig("IPlugs");
PlugSqlOp context = new PlugSqlOp(sql.ToString());
foreach(IPlugStarClub plugin in m_plugins)
{
if(plugin.Name==plugname)
{
plugin.PerformUpdate(context);
return ;
}
}
}
private OleDbDataReader dr;
public OleDbDataReader PerformSelect(string plugname,StringBuilder sql)//执行查询操作
{
//OleDbDataReader dr=new OleDbDataReader();
m_plugins = (PluginCollection )ConfigurationSettings.GetConfig("IPlugs");
PlugSqlOp context = new PlugSqlOp(sql.ToString());
foreach(IPlugStarClub plugin in m_plugins)
{
if(plugin.Name==plugname)
{
dr=plugin.PerformSelect(context);
return dr;
}
}
return dr;
}
//如何使用上面的功能:先在前端文件中生成如下代理:
//public delegate void PerformInsert(string plugname,StringBuilder sql);
//public delegate void PerformDelete(string plugname,StringBuilder sql);
//public delegate OleDbDataReader PerformSelect(string plugname,StringBuilder sql);
//public delegate void PerformUpdate(string plugname,StringBuilder sql);
/* 插入示例
StringBuilder sql=new StringBuilder();
sql.Append("insert into clubuser (ID,sessionID,username) values (2,'123123','fuck')");
Common InsertUser=new Common();
PerformInsert Insert=new PerformInsert(InsertUser.PerformInsert);
Insert("clubstar.IPlug.DataOp.User",sql); //调用插件所代理的方法
*/
/* 删除示例
StringBuilder sql=new StringBuilder();
sql.Append("delete from clubuser where ID=2");
Common DeleteUser=new Common();
PerformDelete Delete=new PerformDelete(DeleteUser.PerformDelete);
Delete("clubstar.IPlug.DataOp.User",sql); //调用插件所代理的方法
*/
/* 查询示例
StringBuilder sql=new StringBuilder();
sql.Append("select * from clubuser where ID=2");
Common SelectUser=new Common();
PerformSelect Select=new PerformSelect(SelectUser.PerformSelect);
dr=Select("clubstar.IPlug.DataOp.User",sql); //调用插件所代理的方法
do{
Response.Write(dr["username"].ToString()+"<br>");
Response.Write(dr["nickname"].ToString()+"<br><br>");
}
while(dr.Read());
*/
/* 更新示例
StringBuilder sql=new StringBuilder();
sql.Append("update clubuser set ID=3 where ID=2");
Common UpdateUser=new Common();
PerformUpdate Update=new PerformUpdate(UpdateUser.PerformUpdate);
Update("clubstar.IPlug.DataOp.User",sql); //调用插件所代理的方法
*/
}
}
最后,只需要在WEBCONFIG中写入插件的信息就可以调用前面所写的插件了:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="IPlugs" type="clubstar.PluginSectionHandler, clubstar" />
</configSections>
<IPlugs>
<plugin type="clubstar.IPlug.DataOp.User" />
</IPlugs>.........
由于这是本人第一次写文章,加之时间仓促,望各个大侠见谅。