WEB program design based on unified plug-in Interface

Source: Internet
Author: User
  File Name : User. CS
Using system;
Using system. Data;
Using system. Data. oledb;
// Using clubstar. iplug;
Using clubstar;
Namespace clubstar. iplug. dataop
{
Public struct usertable // User table structure variable
{
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>
/// Summary of the user.
/// </Summary>
///
Public class user: iplugstarclub
{
  
Public conndb_op usertable = conndb_op. Instance (); // Call the single mode for database connection
  
Protected string sqlstring = "";
Public user ()
{
  
  
}
  
Public User (string SQL)
{
SqlString = SQL;
  
}
# Region IPlugStarClub Members
/// <Summary>
/// Return The Name Of The called plug-in
/// </Summary>
Public string Name
{
Get
{
// Return "User_Plugin: User table plug-in ";
Return "clubstar. IPlug. DataOp. User ";
}
}
/// <Summary>
/// User table operations
/// </Summary>
/// <Param name = "context"> </param>
Public OleDbDataReader extends mselect (IPlugSql SQL)
{
// SQL. SqlString = SqlString;
Return UserTable. ExecuteReader (SQL. SqlString );
}
Public void merge mdelete (IPlugSql SQL)
{
// SQL. SqlString = SqlString;
UserTable. ExecuteNonQuery (SQL. SqlString );
}
Public void shortminsert (IPlugSql)
{
// SQL. SqlString = SqlString;
UserTable. ExecuteNonQuery (SQL. SqlString );
}
  
Public void merge mupdate (IPlugSql)
{
// SQL. SqlString = SqlString;
UserTable. ExecuteNonQuery (SQL. SqlString );
}
# Endregion
}
}
The writing of the database connection class is as follows:
File Name: 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 abstract description.
/// Use the Facade design mode to encapsulate all database-related operations
/// </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. deleettings ["clubstar"];
  
// The Singleton mode is used for database connection. You can only use Instance () to create a unique Instance of this class.
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 ()
{
  
}........
The following files are used to handle the operations of the plug-in.
File Name: 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
// Passing in the type name specified in the attribute value
Object plugobject = activator. createinstance (type. GetType (node. attributes ["type"]. Value); // read the corresponding configuration section in 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
}
}
The following file is used to obtain the plug-in name information:
File Name: PlugSqlOp. cs
Using System;
Using clubstar. IPlug;
Namespace clubstar
{
/// <Summary>
/// Summary of 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
}
}

The following is the original file of the actually called program.
   Us Ing 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>
/// Summary of 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 implements minsert (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, '20170101', 'fuck ')");
Common InsertUser = new Common ();
Using minsert Insert = new using minsert (InsertUser. Using minsert );
Insert ("clubstar. IPlug. DataOp. User", SQL); // call the method represented by the plug-in
  
}
  
# Generated by region web Form Designer Code
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
  
/// <Summary>
/// The designer supports the required methods-do not use code Editor Modify
/// Content of this method.
/// </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)
{
}
}
}
The upload minsert proxy in the above file operates on the following event functions:
Using System;
Using System. Configuration;
Using clubstar. IPlug;
Using System. Text;
Using System. Data. OleDb;
Namespace clubstar
{
/// <Summary>
/// Summary of Common.
/// </Summary>
Public class Common
{
Public Common ()
{
  
}
Private PluginCollection m_plugins = null; // The callback is used to insert the plug-in.
Public void merge minsert (string plugname, StringBuilder SQL) // execute the insert operation
{
M_plugins = (PluginCollection) ConfigurationSettings. GetConfig ("IPlugs ");
PlugSqlOp context = new PlugSqlOp (SQL. ToString ());
Foreach (IPlugStarClub plugin m_plugins)
{
If (plugin. Name = plugname)
{
Plugin. cmdminsert (context );
Return;
}
}
}
Public void merge mdelete (string plugname, StringBuilder SQL) // perform the delete operation.
{
M_plugins = (PluginCollection) ConfigurationSettings. GetConfig ("IPlugs ");
PlugSqlOp context = new PlugSqlOp (SQL. ToString ());
Foreach (IPlugStarClub plugin m_plugins)
{
If (plugin. Name = plugname)
{
Plugin. delemdelete (context );
Return;
}
}
}
Public void merge mupdate (string plugname, StringBuilder SQL) // execute the update operation
{
M_plugins = (PluginCollection) ConfigurationSettings. GetConfig ("IPlugs ");
PlugSqlOp context = new PlugSqlOp (SQL. ToString ());
Foreach (IPlugStarClub plugin m_plugins)
{
If (plugin. Name = plugname)
{
Plugin. Merge mupdate (context );
Return;
}
}
}
Private OleDbDataReader dr;
Public OleDbDataReader extends mselect (string plugname, StringBuilder SQL) // executes the query operation
{
// OleDbDataReader dr = new OleDbDataReader ();
M_plugins = (PluginCollection) ConfigurationSettings. GetConfig ("IPlugs ");
PlugSqlOp context = new PlugSqlOp (SQL. ToString ());
Foreach (IPlugStarClub plugin m_plugins)
{
If (plugin. Name = plugname)
{
Dr = plugin. PerformSelect (context );
Return dr;
}
}
Return dr;
}
  
// How to use the above function: first generate the following proxy in the front-end file:
// Public delegate void implements minsert (string plugname, StringBuilder SQL );
// Public delegate void merge mdelete (string plugname, StringBuilder SQL );
// Public delegate OleDbDataReader extends mselect (string plugname, StringBuilder SQL );
// Public delegate void merge mupdate (string plugname, StringBuilder SQL );
/* Insert example
StringBuilder SQL = new StringBuilder ();
SQL. Append ("insert into clubuser (ID, sessionID, username) values (2, '20170101', 'fuck ')");
Common InsertUser = new Common ();
Using minsert Insert = new using minsert (InsertUser. Using minsert );
Insert ("clubstar. iplug. dataop. User", SQL); // call the method represented by the plug-in
*/
/* Deletion example
Stringbuilder SQL = new stringbuilder ();
SQL. append ("delete from clubuser where id = 2 ");
Common deleteuser = new common ();
Required mdelete delete = new parameter mdelete (deleteuser. delemdelete );
Delete ("clubstar. iplug. dataop. User", SQL); // call the method represented by the plug-in
*/
  
/* Query example
Stringbuilder SQL = new stringbuilder ();
SQL. append ("select * From clubuser where id = 2 ");
Common selectuser = new common ();
Optional mselect select = new performselect (selectuser. performselect );
Dr = select ("clubstar. iplug. dataop. User", SQL); // call the method represented by the plug-in
  
  
Do {
Response. Write (Dr ["username"]. tostring () + "<br> ");
Response. Write (dr ["nickname"]. ToString () + "<br> ");
}
While (dr. Read ());
*/
/* Update example
StringBuilder SQL = new StringBuilder ();
SQL. Append ("update clubuser set ID = 3 where ID = 2 ");
Common UpdateUser = new Common ();
Required mupdate Update = new required mupdate (UpdateUser. Required mupdate );
Update ("clubstar. IPlug. DataOp. User", SQL); // call the method represented by the plug-in
*/
}
}
Finally, you only need to write the plug-in information in WEBCONFIG to call the plug-in previously written:
<? 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> .........

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.