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

类别:.NET开发 点击:0 评论:0 推荐:


接上回:
在这一次所写的代码是所谓的插件,因为实际的插件文件应该是DLL或一些被编辑过的文件格式[各个公司都有自己的命名规则],本人在这里偷懒,只用简单的CS原文件加以代替
文件名: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
 }
}

本文地址:http://com.8s8s.com/it/it42407.htm