基于统一插件接口的WEB程序设计

 文件名:User.cs
  using System;
  using System.Data;
  using System.Data.OleDb;
  //using clubstar.IPlug;
  using clubstar;
  namespace clubstar.IPlug.DataOp
  {
  public struct UserTable //用户表结构变量
  {
   public int id; //
   public int sessionid;
   public string name;
   public string nickname; //
   public string password; //
   public bool sex; //
   public DateTime birthday; //
   public string city; //
   public int weight; //
   public int height; //
   public string education; //
   public int bloodtype; //
   public string xingzuo;
   public bool marry;
   public string job; //
   public string favorstar; //
   public string favordo; //
   public string introduce; //
   public string email; //
   public DateTime joindate; //
   public DateTime logindate; //
   public int rank; //
  
   public string mobilephone; //
   public string pic; //
   public string pic_id; //
   public int jifen; //
   public int renqi; //
   public int acceptwho; //
  }
  /// <summary>
  /// User 的摘要说明。
  /// </summary>
  ///
  public class User: IPlugStarClub
  {
  
   public ConnDB_Op UserTable=ConnDB_Op.Instance(); //调用单体模式进行数据库的连接
  
   protected string SqlString="";
   public User()
   {
  
  
   }
  
   public User(string sql)
   {
   SqlString=sql;
  
   }
   #region IPlugStarClub Members
   /// <summary>
   /// 返回调用插件的名称
   /// </summary>
   public string Name
   {
   get
   {
   //return "User_Plugin:用户表插件";
   return "clubstar.IPlug.DataOp.User";
   }
   }
   /// <summary>
   ///用户表操作
   /// </summary>
   /// <param name="context"></param>
   public OleDbDataReader PerformSelect (IPlugSql sql)
   {
   //sql.SqlString=SqlString;
   return UserTable.ExecuteReader(sql.SqlString);
   }
   public void PerformDelete (IPlugSql sql)
   {
   //sql.SqlString=SqlString;
   UserTable.ExecuteNonQuery(sql.SqlString);
   }
   public void PerformInsert (IPlugSql sql)
   {
   //sql.SqlString=SqlString;
   UserTable.ExecuteNonQuery(sql.SqlString);
   }
  
   public void PerformUpdate (IPlugSql sql)
   {
   //sql.SqlString=SqlString;
   UserTable.ExecuteNonQuery(sql.SqlString);
   }
   #endregion
  }
  }
  其中关于数据库连接类的写法如下
  文件名:ConnDB_Op.cs
  using System;
  using System.Data;
  using System.Data.SqlClient;
  using System.Data.OleDb;
  using System.Configuration;
  namespace clubstar.IPlug.DataOp
  {
  /// <summary>
  /// ConnDB_Op 的摘要说明。
  /// 采用 Facade 外观设计模式以封装所有与数据库相关的操作
  /// </summary>
  public class ConnDB_Op
  {
   private OleDbDataAdapter da=new OleDbDataAdapter();
   private DataSet ds=new DataSet();
   private OleDbCommand cmd=new OleDbCommand();
   public OleDbConnection MyConn;
   public OleDbDataReader dr;
   public string SqlString="";
   public string StrConn="Provider=SQLOLEDB;"+ConfigurationSettings.AppSettings["clubstar"];
  
   //数据库连接采用Singleton 模式,只能通过Instance()来创建该类的唯一实例
   private static ConnDB_Op conndb_op = null;
   public static ConnDB_Op Instance()
   {
   if (null == conndb_op)
   conndb_op = new ConnDB_Op();
   return conndb_op;
   }
   private ConnDB_Op(string strconn,string sqlstring)
   {
   StrConn=strconn;
   SqlString=sqlstring;
   }
   private ConnDB_Op()
   {
  
   }........
  如下的文件用于实际处理插件的操作
  文件名:PluginSectionHandler.cs
  using System;
  using System.Xml;
  using System.Configuration;
  using clubstar.IPlug;
  namespace clubstar
  {
  /// <summary>
  /// This class implements IConfigurationSectionHandler and allows
  /// us to parse the "plugin" XML nodes found inside App.Config
  /// and return a PluginCollection object
  /// </summary>
  public class PluginSectionHandler:IConfigurationSectionHandler
  {
   public PluginSectionHandler()
   {
  
   }
   #region IConfigurationSectionHandler Members
   /// <summary>
   /// Iterate through all the child nodes
   /// of the XMLNode that was passed in and create instances
   /// of the specified Types by reading the attribite values of the nodes
   /// we use a try/Catch here because some of the nodes
   /// might contain an invalid reference to a plugin type
   /// </summary>
   /// <param name="parent"></param>
   /// <param name="configContext"></param>
   /// <param name="section">The XML section we will iterate against</param>
   /// <returns></returns>
   ///
   public System.Web.HttpResponse dai;
   public object Create(object parent, object configContext, System.Xml.XmlNode section)
   {
   PluginCollection plugins = new PluginCollection();
   foreach(XmlNode node in section.ChildNodes)
   {
  
   try
   {
   //Use the Activator class's 'CreateInstance' method
   //to try and create an instance of the plugin by
   //passing in the type name specified in the attribute value
   object plugObject = Activator.CreateInstance(Type.GetType(node.Attributes["type"].Value));//读取WEBCONFIG中的相应配置节
  
   //Cast this to an IPlugin interface and add to the collection
   IPlugStarClub plugin = (IPlugStarClub)plugObject;
   plugins.Add(plugin);
  
   }
   catch
   {
   //Catch any exceptions
   //but continue iterating for more plugins
   //dai.Write(section.ChildNodes.Count.ToString());
   throw;
   }
  
   }
  
  
   return plugins;
   }
   #endregion
  }
  }
  而如下文件用于获取插件的名字信息:
  文件名:PlugSqlOp.cs
  using System;
  using clubstar.IPlug;
  namespace clubstar
  {
  /// <summary>
  /// UserOp 的摘要说明。
  /// </summary>
  public class PlugSqlOp:IPlugSql
  {
   private string m_User="";
   public PlugSqlOp(string user)
   {
   m_User = user;
   }
   #region PlugSqlOp Members
   public string SqlString
   {
   get
   {
   return m_User;
   }
   set
   {
   m_User = value;
   }
   }
   #endregion
  }
  }



下面就是实际调用的程序原文件
  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>.........
posted @ 2009-09-13 01:02  星火卓越  阅读(517)  评论(0编辑  收藏  举报