版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、現(xiàn)在很多項忖都須要為將來的擴展考慮,當(dāng)然數(shù)據(jù)庫也是一個很重我要的方面,擴展門c的provider,這就需耍反射技術(shù),雖然會對性能有所影響,但是性價比還是很高的哦,從petshop和communityserver都可以看到反射技術(shù)啦,也可以說反射是最基木的啦,呵呵!他的老家是在system.reflection,當(dāng)我們要開工吋首先就是要把他抓出來.要實現(xiàn)這一功能我們當(dāng)然要知道provider的程序集,和工作的類名了,在多層架夠屮才能讓邏 輯和數(shù)據(jù)進行溝通,這樣也方便團隊開發(fā)的協(xié)條款發(fā)展,我們通過petshop和 communityserver兩個例子來說明一下.我們先看看petshop的反射技術(shù)
2、,在配制文件屮發(fā)現(xiàn)如下配制:v pet shop dal configuration settings -><add key= "webdal'1 value= "petshop.sqlserverdal'7><add key= "ordersdal" value= "petshop.sqlserverdal7><add key= "profiledal" value= "petshop.sqlprofiledal"/>其實只從配制文件中得到程序集
3、名稱,一般程序集就是類所在命名空間,也就是編譯后顯示的 dll名稱,那petshop是怎樣工作的,下面我們來看一下dataaccess類,這也】j說成一個工廠, 呵呵,我們來看一下代碼:1 using system.reflection;2 using system .configuration;33 曰國 namespace pet shop. dalfactory 56申由 iii < summary>7 iii this class is implemented following the abstract factory pattern to creat e the dal
4、 implementation8 iii specified from the configuration file9 / </summary>10申由 public sealed class dataaccess 12 / look up the dal implementation we should be using13 private static read only string path = configuratio nmanager.appsetti ngs” webdal”;14 private static read only string orderpath =
5、 con figuratio nmanager.appset tingshordersdalm;1516申申 private dataaccess() 1718:勺 public static petshop. i dal. i category createcategory() 19 string classname = path + ".category”;20 return (petshop.l dal. i category) assembly. load (path). creat el nstanc e(classname);21 f 2223審申 public stat
6、ic petshop.i dal.i inventory createlnventory() 24 string classname = path + tlnventory11;25 return (petshop.ldaljinventory)assembly.load(path)createlnstanc e(classname);26卜2728 *申 public static petshop.idal.litem createltem() 29 string classname = path + ".iterrt;30 return (petshop.l dal. 11 te
7、rn) assembly. load (path) .creat el n st a nce(cla ssname);31 3232 勻中 public static petshop. i dal. i order createorder() 33 string classname = orderpath + h.order*1;34 return (petshop.l dal. i order) assembly. load (order path). creat el nst an ce(classname);36卜3738 v 中 public static petshop. i dal
8、. i product createproduct() 39 string classname = path + product40 return (petshop.l dal. i product) assembly. load (path). creat el nstance (classname);41 f 4243卜44 l其中(petshop.l dal. i product) assembly. load (path) .creat el nstance( classname);就是將類進行反射,首先要載入程序集,然厲再創(chuàng)進類實例,通過靜態(tài)方法就可以玄接調(diào)用接口的 方法等,從而實現(xiàn)
9、了繼承接口的類的反射,同時也方便表現(xiàn)層的數(shù)據(jù)傳輸.下血我們來看一下communityserver是怎么實現(xiàn)的,cs是一個大彖極別的項h,所以他有 很h己擴展provider,那怎么樣才能讓他們工作呢?其實原理和上述的反射方法差不多,只不過 cs用的是object.gettyp()而達到這項功能.當(dāng)我第一次看到時,還一直為怎樣傳輸 connectionstring&dataowner而不解,厲來打開源數(shù)擁看了一卜-各個方法的注解后才理解.cs從自定的配制文件屮讀取provider節(jié)點并緩存,這在csconfiguration類屮可以發(fā)現(xiàn),在 dataproviders類中就實現(xiàn)反射,通過
10、能構(gòu)造函數(shù)的反射查找最兀配的構(gòu)造函數(shù)對類進行實例化, 當(dāng)然當(dāng)他們遇到有connectionstring&dataowner兩參數(shù)的構(gòu)造函數(shù)時,就對其進行實例化, 從而達到對他們值的傳輸.1 /2 / <copyright company= "telligent systems">3 / copyright (c) telligent systems corporation. all rights reserved.4 / </copyright>5 /7 using system;8 using system.configuration;9
11、using system.io;10 using system.reflection;11 using system.web;12 using comm un ityserver.c on figuration;1313 namespace comm unity server. com pone nts15曰囹16白申 / < summary>17/ dataproviders is responible for loading and managing the various cs dataproviders1 8 - iii </summary>19 public
12、sealed class dataproviders20® 申21韜 / <summary>22 iiithis class cannot be instantiated23 _iii</summary>24 private dataproviders()25胡26卜27 i28 private static void getdatastoreparameters(provider dataprovider, out string connectionstring, out string databaseowner)29® 申30 databaseo
13、w ner = dataprovider.attributeshdatabaseow ner”;31 if(databaseowner = null 11 databaseowner.trim().length = = 0)32databaseow ner = con figurationsetti ngs.appsettingsdataprovider.attributes "databaseow nerst ring name”;33 i34 conn ectio nstring = dataprovider. attributes! hco nn ectio nstri ng”
14、;35 if(corrnectionstring = = null 11 cormectionstring.trim().length = = 0)36 corm ectio nstri ng = con figurationsetti ngs.appsetti ngsdataprovid er.attributesnc onn ectio nstri ngname”;37 f 38 i39誚iii < summary>40 / creates an in st a nee of the provider using activator this in stance sh ould
15、 be41 i / cached since it is an expesivie operation42 'iii </summary>43 public static object create in st a nce(provider dataprovider)44誚 45 /find the current attributes46 string cormectionstring = null; /dataprovider.attributeshconnection string*;47 string databaseowner = null;/ dataprovi
16、der.attributeshdatabaseow nerh;48 i49 getdatastoreparameters(dataprovider, out cormectionstring,out da tabaseowner);50 i51 /get the type52 type type = type.gettype(dataprovider.type);53 i54 object newobject=null;55 if(type != null)56帕newobject = activator.createlnstance(type,new object databaseowner
17、,connectionstring);58 f59 i60 if(newobject = = null) /if we can not create an instance, throw an e xceptio n61 providerexceptio n( dataprovider.name);62 i63 return newobject;64卜65 i66®申iii < summary>67 / creates and caches the constructor!nfo for the specified provider.68 - iii </summa
18、ry>69 public static constructorinfo creat econstructor info (provider dataprovi der)70® 申71 i72 / the assembly should be in bin or gac, so we simply need73 / to get an instance of the type74 i/75 csconfiguration config = csconfiguration.getconfig();76 constructorinfo providercnstr = null;77
19、try78誚79/string providertypename = (provider) configprovidersprovide rname).type;80type type = typegettype( dataprovider.type );type paramtypes = new type2;paramtypes0 = typeof(stri ng);paramtypes 1 = typeof(stri ng);providerc nstr = type. get construct or (param types);catchprovider except ion (dat
20、aprovider. name);if(providerc nstr= n ull)providerexcepti on( dataprovider.name);return providercnstr;/ < summary>iii creates an in st a nee of the specified provider using the cached/ constructorlnfo from createconstructorlnfoiii </summary>public static object invoke(provider dataprovid
21、er)object param array = new object2;string dbowner = null; string connstring = null;getdatastoreparameters(dataprovider, out conn string, out dbow ne param array0 = dbow ner;param array1 = conn stri ng;return createc on structorlnfo(dataprovider).! nvoke( param array);# region exceptionprivate static void providerexception(string
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五版龍門吊租賃及吊裝作業(yè)風(fēng)險分擔(dān)協(xié)議3篇
- 二零二五年四人共同經(jīng)營民宿的合伙協(xié)議書
- 二零二五年度出租車車輛租賃與智能駕駛技術(shù)研發(fā)合同3篇
- 二零二五年度展會現(xiàn)場搭建及展品運輸合同3篇
- 2025年度高空作業(yè)安全防護施工合同范本4篇
- 二零二五年度城市綠化養(yǎng)護承包合同范本8篇
- 2025年度電動汽車充電樁安全檢測與維護服務(wù)合同3篇
- 2025年新媒體營銷活動合作協(xié)議范本2篇
- 2025年度泥瓦工勞務(wù)分包合同工期延誤責(zé)任協(xié)議
- 2025版農(nóng)業(yè)機械銷售訂購合同(年度版)3篇
- 2024年合肥市廬陽區(qū)中考二模英語試題含答案
- 質(zhì)檢中心制度匯編討論版樣本
- 藥娘激素方案
- 提高靜脈留置使用率品管圈課件
- GB/T 10739-2023紙、紙板和紙漿試樣處理和試驗的標(biāo)準(zhǔn)大氣條件
- 《心態(tài)與思維模式》課件
- C語言程序設(shè)計(慕課版 第2版)PPT完整全套教學(xué)課件
- 行業(yè)會計比較(第三版)PPT完整全套教學(xué)課件
- 高考英語語法填空專項訓(xùn)練(含解析)
- 危險化學(xué)品企業(yè)安全生產(chǎn)標(biāo)準(zhǔn)化課件
- 《美的歷程》導(dǎo)讀課件
評論
0/150
提交評論