使用 Castle ActiveRecord

本文介绍如何使用 Castle Active Record 进行数据库操作,包括从数据库生成映射类及生成数据库表结构的方法,并提供了配置示例及简单使用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用 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

p1.jpg
   - "Project Explorer" -> "Add DataBase Connection" 做好设定,连接你的 database, 按 ok (p1)


p2.jpg
   - "ActiveRecord Components" -> 拖拉 "ActiveRecord" 去左方空白 (p2)

p3.jpg
   - "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 方式生成配置:

            ActiveRecordStarter.Initialize(
                
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值