分享
 
 
 

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

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

接上回:

在这一次所写的代码是所谓的插件

,因为实际的插件文件应该是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

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有