使用 Castle ActiveRecord
使用 Castle ActiveRecord:
? 由 database (測試于SQL 2005) 直接使用 AR 的 Generator 產生 mapping class (C#/BV.Net) 法:
?(前題是你以建好了 database)
?
?? - 安裝完成后,執行 C:\Program Files\Castle\Bin\net-2.0\Castle.ActiveRecord.Generator.exe
?? - "Project Explorer" -> "Add DataBase Connection" 做好設定,連接你的 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 一至多個
?? - ActiveRecordStarter.CreateSchema();
???? 自動生成 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, 有一個 pk (id) 和一個 Property (name) )?
????using?(new?SessionScope())
????{
???????DemoClassA?dp?=?new?DemoClassA();
???????dp.Name?=?"工程部";
???????dp.Save();
????}
?總結:
?? - 對于小型的工程,是不錯的輕量級快速解決方案
?? - AR 基層代碼量少,無多余設定,清晰
?? - 提供自動 generator 工具,生產力提升不少 (尢其是配合 power designer)
?? - 可以不使用 xml 配置,更不易配置出錯
?? - 大部份反回的 error msg 是 NHibernate 的 error msg 因為 Castle 的 ActiceRecord 是封裝他的,
???? 而如果你不具備 NHibernate 的知識,可能會不知所云.(我就是沒用 NHibernate 故覺得其 error msg 很煩人)
?? - 對于復雜的統計很難做的好,效率也不太高
?? - 對于中大型的工程,工程的復雜程度使你用時有綁手綁腳的感覺
?? - 對于代碼安全程度也是偏低,因為使用了大量的 DLL (尢其全是開源的),幾乎暴露了所有
???? logic / business rule, 假如只是自己公司使用而非開發出售的公司會好一點點...
???? 但是如果是大型的工程,因投入的資金和人力,代碼安全應是首要考慮,老板可是看得好緊.
???? (值得一提的是 AR 因為使用了 attributes 使你可以使用混淆器,而 NHibernate
????? 使用 reflection 使你基本上不能對 data class (mapping class) 進行混淆)
?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
轉載于:https://www.cnblogs.com/adamver/archive/2006/03/22/356104.html
總結
以上是生活随笔為你收集整理的使用 Castle ActiveRecord的全部內容,希望文章能夠幫你解決所遇到的問題。