单系统多类型数据源随意切换的c#实现

【原创作者】:丛兴滋(cncxz)[E-mail:cncxz@126.com]
【关 键 词】:SQL  XML  Access  C#  切换

    假设你要做个会员管理系统,分为SQL、XML和Access三种版本,希望通过修改配置文件中一个参数来完成版本切换,而目前你又对这种切换的实现存有疑惑,建议你继续阅读,本文说明了“一个接口、三个实现、一个代理类结合配置文件实现SQL、XML、Access数据源切换”的一种方法。

下面以会员管理系统的单位管理为例,说一下这种方法:

1、首先定义一个接口IDeptManage,代码如下:

None.gif // 接口IDeptManage
ExpandedBlockStart.gifContractedBlock.gif
public   interface  IDeptManage dot.gif {
InBlock.gif
InBlock.gif 
void ItemCreate(string strGuid,string strShortName,string strFullName,string strParentGuid); 
InBlock.gif
InBlock.gif 
void ItemRemove(string strGuid);
InBlock.gif
InBlock.gif 
void ItemUpdate(string strGuid,string strShortName,string strFullName);
InBlock.gif
InBlock.gif 
string test();//测试用
ExpandedBlockEnd.gif
}



2、然后写三个互相独立的实现类,DeptManage_Sql、DeptManage_Xml、DeptManage_Access,代码如下:

None.gif // 类DeptManage_Sql,用SQL的实现
None.gif
public   class  DeptManage_Sql:IDeptManage
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemCreate(string strGuid,string strShortName,string strFullName,string strParentGuid)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemRemove(string strGuid)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemUpdate(string strGuid,string strShortName,string strFullName)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public string test()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return "这个是用SQL实现的";
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gif
// 类DeptManage_Xml,用XML的实现
None.gif
public   class  DeptManage_Xml:IDeptManage
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemCreate(string strGuid,string strShortName,string strFullName,string strParentGuid)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemRemove(string strGuid)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemUpdate(string strGuid,string strShortName,string strFullName)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public string test()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return "这个是用XML实现的";
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gif
// 类DeptManage_Access,用Access的实现
None.gif
public   class  DeptManage_Access:IDeptManage
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemCreate(string strGuid,string strShortName,string strFullName,string strParentGuid)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemRemove(string strGuid)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
public void ItemUpdate(string strGuid,string strShortName,string strFullName)dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  
public string test()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return "这个是用Access实现的";
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif }

None.gif


    以前的时候,都是用 IDeptManage myClass=new DeptManage_Sql()跟IDeptManage myClass=new DeptManage_Xml()来实现调用,这在使用中似乎也没有什么不妥,可是一旦需要切换(假设要把Sql的换成Xml),就麻烦了,需要到处修改代码,现在我要做的就是把这些在切换时需要调整的部分集中起来,达到简单地调整一处代码就完成切换工作的目的。

3、其实实现起来非常简单,只要在调用者和调用源中间补上一个中介人实现解耦就可以了,这里定义一个名为Agent的代理类,代码如下:

None.gif public   class  Agent
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif {
InBlock.gif  
static int DataType=0;  //数据库类型[0为SQL、1为XML、2为Access]
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif  
public static IDeptManage CreateDeptMange()dot.gif{
InBlock.gif   IDeptManage myClass;
ExpandedSubBlockStart.gifContractedSubBlock.gif   
switch (DataType)dot.gif{
InBlock.gif    
case 0:
InBlock.gif     myClass
=new DeptManage_Sql();
InBlock.gif     
break;
InBlock.gif    
case 1:
InBlock.gif     myClass
=new DeptManage_Xml();
InBlock.gif     
break;
InBlock.gif
InBlock.gif    
case 2:
InBlock.gif     myClass
=new DeptManage_Access();
InBlock.gif     
break;
InBlock.gif    
default:
InBlock.gif     myClass
=new DeptManage_Sql();
InBlock.gif     
break;
ExpandedSubBlockEnd.gif   }

InBlock.gif   
return myClass;
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif }

None.gif


    以后调用的时候改用IDeptManage myManage= Agent.CreateDeptMange()就可以了,想切换的时候修改Agent类中的DataType即可。


    修改DataType值,通过调用 myManage.test()可以查看当前使用的哪个实现类,把Agent类调整一下,从web.config里读DataType值,最终就通过一个接口、三个实现、一个代理类结合配置文件实现了SQL、XML、Access数据源的切换,这里不写了,你自己来吧    :)

posted on 2005-11-30 11:06  cncxz(虫虫) 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/cncxz/archive/2005/11/30/287625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值