使用 Castle ActiveRecord
使用 Castle ActiveRecord:
? 由 database (測試于SQL 2005) 直接使用 AR 的 Generator 產(chǎn)生 mapping class (C#/BV.Net) 法:
?(前題是你以建好了 database)
?
?? - 安裝完成后,執(zhí)行 C:\Program Files\Castle\Bin\net-2.0\Castle.ActiveRecord.Generator.exe
?? - "Project Explorer" -> "Add DataBase Connection" 做好設(shè)定,連接你的 database, 按 ok (p1)
?? - "ActiveRecord Components" -> 拖拉 "ActiveRecord" 去左方空白 (p2)
?? - "Project" -> "Generate Code" -> 輸入 name space, output dir, code type, ok (p3)
?由 mapping class 生成 database table struct 的方法:
?? - 寫好你的 mapping class 一至多個(gè)
?? - ActiveRecordStarter.CreateSchema();
???? 自動(dòng)生成 database (前題是你以 set 好 ActiveRecordStarter config)
?ActiveRecordStarter config code 方式生成配置:
????????????InPlaceConfigurationSource?source?=?new?InPlaceConfigurationSource();????????????Hashtable?properties?=?new?Hashtable();
????????????properties.Add("hibernate.connection.driver_class",?
???????????????????????????"NHibernate.Driver.SqlClientDriver");
????????????properties.Add("hibernate.dialect",?
???????????????????????????"NHibernate.Dialect.MsSql2000Dialect");
????????????properties.Add("hibernate.connection.provider",?
???????????????????????????"NHibernate.Connection.DriverConnectionProvider");
????????????properties.Add("hibernate.connection.connection_string",?
??????????????????"Data?Source=127.0.0.1;Initial?Catalog=testDB;Integrated?Security=SSPI");
????????????source.Add(typeof(ActiveRecordBase),?properties);
????????????ActiveRecordStarter.Initialize(source,?typeof(??????DemoClassA?????));
?ActiveRecordStarter config xml 方式生成配置:
????????????????new?XmlConfigurationSource("..//app.xml"),
????????????????typeof(DemoClassA));
?
<?xml?version="1.0"?encoding="utf-8"??><activerecord>
????<config>
????????<add?
????????????key="hibernate.connection.driver_class"????????
????????????value="NHibernate.Driver.SqlClientDriver"?/>
????????<add?
????????????key="hibernate.dialect"????????????????????????
????????????value="NHibernate.Dialect.MsSql2000Dialect"?/>
????????<add?
????????????key="hibernate.connection.provider"????????????
????????????value="NHibernate.Connection.DriverConnectionProvider"?/>
????????<add?
????????????key="hibernate.connection.connection_string"????
????????????value="Data?Source=127.0.0.1;Initial?Catalog=testDB;Integrated?Security=SSPI"?/>
????</config>
</activerecord>
?簡單用法( eg: DemoClassA = mapping class, 有一個(gè) pk (id) 和一個(gè) Property (name) )?
????using?(new?SessionScope())
????{
???????DemoClassA?dp?=?new?DemoClassA();
???????dp.Name?=?"工程部";
???????dp.Save();
????}
?總結(jié):
?? - 對于小型的工程,是不錯(cuò)的輕量級快速解決方案
?? - AR 基層代碼量少,無多余設(shè)定,清晰
?? - 提供自動(dòng) generator 工具,生產(chǎn)力提升不少 (尢其是配合 power designer)
?? - 可以不使用 xml 配置,更不易配置出錯(cuò)
?? - 大部份反回的 error msg 是 NHibernate 的 error msg 因?yàn)?Castle 的 ActiceRecord 是封裝他的,
???? 而如果你不具備 NHibernate 的知識(shí),可能會(huì)不知所云.(我就是沒用 NHibernate 故覺得其 error msg 很煩人)
?? - 對于復(fù)雜的統(tǒng)計(jì)很難做的好,效率也不太高
?? - 對于中大型的工程,工程的復(fù)雜程度使你用時(shí)有綁手綁腳的感覺
?? - 對于代碼安全程度也是偏低,因?yàn)槭褂昧舜罅康?DLL (尢其全是開源的),幾乎暴露了所有
???? logic / business rule, 假如只是自己公司使用而非開發(fā)出售的公司會(huì)好一點(diǎn)點(diǎn)...
???? 但是如果是大型的工程,因投入的資金和人力,代碼安全應(yīng)是首要考慮,老板可是看得好緊.
???? (值得一提的是 AR 因?yàn)槭褂昧?attributes 使你可以使用混淆器,而 NHibernate
????? 使用 reflection 使你基本上不能對 data class (mapping class) 進(jìn)行混淆)
?castle 2.0 (support .net 2.0 and visual studio 2005) msi file:
?? http://prdownloads.sourceforge.net/castleproject/Castle.msi?download
?src code:
?? http://prdownloads.sourceforge.net/castleproject/Castle-src.zip?download
?example?code and??help doc(english):
?? http://www.castleproject.org/index.php/ActiveRecord:Getting_Started
?? http://www.castleproject.org/index.php/ActiveRecord:Mappings#Class
?? http://www.castleproject.org/index.php/ActiveRecord%2C_.Net_2.0_and_Generics
轉(zhuǎn)載于:https://www.cnblogs.com/adamver/archive/2006/03/22/356104.html
總結(jié)
以上是生活随笔為你收集整理的使用 Castle ActiveRecord的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。