手把手教你写ORM(四)

现在中午不睡一会儿就头晕。

前一篇有人留言说为什么不写web.config?我个人非常反对庞大的web.config文件,可能其根源就在于互联星空系统的巨大的配置和它带来的混乱,自己实现一个小巧灵活的机制是我比较喜欢的,这样可以降低系统的侵入性,也可以方便我改成其他语言的版本。

这里我们来给刚才的Resource类加一个壳,因为我们毕竟不能用绝对的Path去访问,我们需要一个通过类名就可以访问得到配置的方式。

在加这个壳之前我们先规划一下配置文件,我们这里需要两种配置文件,一个是配置数据库连接的,一种是配置每个类的操作的,前边说过了,对输入和结果的映射在类的Attribute里完成,这样子配置每个类的配置文件就可以相当的简单了

数据库连接配置文件:
None.gif <? xml version = " 1.0 "  encoding = " utf-8 "   ?>
None.gif
< config >
None.gif  
< session name = " test " >
None.gif    
< connectionstring > Data Source = .\SQLEXPRESS;AttachDbFilename = " C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\test.mdf " ;Integrated Security = True;Connect Timeout = 30 ;User Instance = True </ connectionstring >
None.gif    
< assambly > Alexander.Xbase.SqlExec </ assambly >
None.gif    
< provider > Alexander.Xbase.SqlExec.Exec </ provider >
None.gif    
< configbase > D:\MyDocuments\Visual Studio  2005 \Projects\Alexander\Xbase\MenTest\bin\Debug\ </ configbase >
None.gif  
</ session >
None.gif
</ config >

每个Session配置一个数据库连接,configbase是用来指定这个连接的配置文件所在的目录,以后获取每个类的配置文件就在这个配置项的值所在的目录里面。数据库配置文件在System.AppDomain.CurrentDomain.BaseDirectory里面搜索,不用配置


每个类的配置文件:
None.gif <? xml version = " 1.0 "  encoding = " utf-8 "   ?>
None.gif
< class >
None.gif    
< sql name = " insert " > insert into tb (aaa,bbb) values (#aaa,#bbb) </ sql >
None.gif    
< sql name = " getall "  cache = " true " > select  *  from tb </ sql >
None.gif
</ class >

Sql节配置SQL指令,参数用#开头,属性Cache指示这个查询是否被缓存

我们需要两个类来分别表示这两种配置文件来提供对其的访问方式。
对于数据库配置文件我们先提供一个类来表示其结构:
 1 None.gif   public   class  SessionParameter
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        private string connectionstring;
 4InBlock.gif
 5InBlock.gif        public string Connectionstring
 6ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn connectionstring; }
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ connectionstring = value; }
 9ExpandedSubBlockEnd.gif        }

10InBlock.gif        private string assambly;
11InBlock.gif
12InBlock.gif        public string Assambly
13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
14ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn assambly; }
15ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ assambly = value; }
16ExpandedSubBlockEnd.gif        }

17InBlock.gif        private string provider;
18InBlock.gif
19InBlock.gif        public string Provider
20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
21ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn provider; }
22ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ provider = value; }
23ExpandedSubBlockEnd.gif        }

24InBlock.gif        private string configbase;
25InBlock.gif
26InBlock.gif        public string Configbase
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn configbase; }
29ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ configbase = value; }
30ExpandedSubBlockEnd.gif        }

31ExpandedBlockEnd.gif    }

32 None.gif}

然后通过下面的类来访问,这里我们统统使用Xpath来搜索节点。
None.gif      public   class  Sessions
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static string _configPath;
InBlock.gif        
private static XmlDocument dat; 
InBlock.gif
InBlock.gif        
static Sessions()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _configPath 
= System.AppDomain.CurrentDomain.BaseDirectory + "xbase.xml";
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public SessionParameter GetSessionByName(string Name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Resource res 
= new Resource(_configPath);
InBlock.gif            dat 
= new XmlDocument();
InBlock.gif            dat.LoadXml(res.Config[_configPath]);
InBlock.gif            
string xp1 = "/config/session[@name=\""+Name+"\"]/connectionstring";
InBlock.gif            
string xp2 = "/config/session[@name=\"" + Name + "\"]/assambly";
InBlock.gif            
string xp3 = "/config/session[@name=\"" + Name + "\"]/provider";
InBlock.gif            
string xp4 = "/config/session[@name=\"" + Name + "\"]/configbase";
InBlock.gif            SessionParameter sp 
= new SessionParameter();
InBlock.gif            sp.Connectionstring 
= dat.SelectSingleNode(xp1).InnerText;
InBlock.gif            sp.Assambly 
= dat.SelectSingleNode(xp2).InnerText.Trim();
InBlock.gif            sp.Provider 
= dat.SelectSingleNode(xp3).InnerText.Trim();
InBlock.gif            sp.Configbase 
= dat.SelectSingleNode(xp4).InnerText.Trim();
InBlock.gif            
return sp;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

这个样子就可以通过Session sec=Session.GetSessionByName(Name)来获取一个数据库配置文件了。
实现读取每个类的配置文件的方式相同,这里省略了,下来自己写

这里我们实现了包装读取配置的操作的类,接下来我们来看,如何实现插件的方式动态提供数据库操作组件

to be continue......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值