Active Record学习笔记(一):初步接触

最近开始接触Castle ActiveRecord,学习资料大部分是从网上找到的。这里要特别感谢TerryLee的系列文章:Castle 开发系列 ,在Castle的学习之路上,这个系列文章对我的影响是十分巨大的!除了这个系列文章之外,Castle的官方网站也是学习Castle的好去处!

本篇学习笔记从一个简单对象的CURD操作入手,介绍ActiveRecord!

主要内容:
1.ActiveRecord概述
2.准备数据表
3.编写实体类
4.编写配置文件
5.对象的CRUD操作
6.表示层调用

一、ActiveRecrod概述
  ActiveRecordCastle中提供的一个数据访问框架,它在底层封装了NHibernate的操作。与NHibernate相比,ActiveRecord使用特性来代替映射文件hbm.xml,它提供的简洁的O/R映射会让你惊叹原来实现持久化数据层是那么简单。

二、准备数据表

None.gif Create   Table   [ Users ]
None.gif(
None.gif    
[ ID ]   Int   Identity ( 1 , 1 Primary   Key ,
None.gif    
[ LoginName ]   Varchar ( 50 not   null ,
None.gif    
[ Password ]   Varchar ( 20 not   null
None.gif)

三、编写实体类User
  1.引用Castle.ActiveRecord.dll组件;
  2.引用Castle.ActiveRecord名称空间:
None.gif using  Castle.ActiveRecord;

  3.让User类继承ActiveRecordBase类(此类处于Castle.ActiveRecord名称空间之下):
None.gif public   class  User : ActiveRecord
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
//dot.gif
ExpandedBlockEnd.gif
}

  4.用[ActiveRecrod()]为类添加特性,指出User类对应的数据表是Users:
None.gif [ActiveRecord( " Users " )]
None.gif
public   class  User : ActiveRecordBase
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
//dot.gif
ExpandedBlockEnd.gif
}

  5.用[Property()]为属性添加特性,指出属性对应数据表中的列:
None.gif [ActiveRecord( " Users " )]
None.gifpublic
class  User : ActiveRecordBase
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private int _id;
InBlock.gif    
private string _name;
InBlock.gif    
private string _password;
InBlock.gif
InBlock.gif    
//[PrimaryKey]特性指定Id作为主键
InBlock.gif    
//用PrimaryKeyType.Identity来说明了主键的类型为自增型的
InBlock.gif
    [PrimaryKey(PrimaryKeyType.Identity, "ID")]
InBlock.gif    
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _id = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    [Property(
"LoginName")]
InBlock.gif    
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _name = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//若属性名与列名一致可省略不写
InBlock.gif
    [Property]
InBlock.gif    
public string Password
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _password; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _password = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

四、编写配置文件App.config或web.config。由于ActiveRecord在底层封装了NHibernate,故配置文件的信息和NHibernate一致。
App.config:
None.gif <? xml version="1.0" encoding="utf-8"  ?>
None.gif
< configuration >
None.gif    
< configSections >
None.gif        
< section  name ="activerecord"  type ="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"   />
None.gif    
</ configSections >
None.gif    
< activerecord >
None.gif        
< config >
None.gif            
< add  key ="hibernate.connection.driver_class"  value ="NHibernate.Driver.SqlClientDriver"   />
None.gif            
< add  key ="hibernate.dialect"  value ="NHibernate.Dialect.MsSql2000Dialect"   />
None.gif            
< add  key ="hibernate.connection.provider"  value ="NHibernate.Connection.DriverConnectionProvider"   />
None.gif            
< add  key ="hibernate.connection.connection_string"  value ="UID=sa;Password=123456789;Initial Catalog=Castle;Data Source=EMMALEE"   />
None.gif        
</ config >
None.gif    
</ activerecord >
None.gif
</ configuration >
web.config
None.gif <? xml version="1.0" ?>
None.gif
< configuration >
None.gif    
< configSections >
None.gif        
< section  name ="activerecord"  type ="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
None.gif    
</ configSections >
None.gif    
< activerecord  isWeb ="true" >
None.gif        
< config >
None.gif            
< add  key ="hibernate.connection.driver_class"  value ="NHibernate.Driver.SqlClientDriver"   />
None.gif            
< add  key ="hibernate.dialect"  value ="NHibernate.Dialect.MsSql2000Dialect"   />
None.gif            
< add  key ="hibernate.connection.provider"  value ="NHibernate.Connection.DriverConnectionProvider"   />
None.gif            
< add  key ="hibernate.connection.connection_string"  value ="UID=sa;Password=123456789;Initial Catalog=Castle;Data Source=EMMALEE"   />
None.gif        
</ config >
None.gif    
</ activerecord >
None.gif    
< system .web >
None.gif        
< compilation  debug ="true" />
None.gif        
< authentication  mode ="Windows" />
None.gif            
< customErrors  mode ="RemoteOnly"  defaultRedirect ="GenericErrorPage.htm" >
None.gif            
< error  statusCode ="403"  redirect ="NoAccess.htm"   />
None.gif            
< error  statusCode ="404"  redirect ="FileNotFound.htm"   />
None.gif        
</ customErrors >
None.gif    
</ system.web >
None.gif
</ configuration >
None.gif

五、对象的CRUD操作。类ActiveRecordBas中定义了许多静态方法用于对象的CRUD操作,如:Create、Delete、DeleteAll、FindAll、FindAllByProperty、FindByPrimaryKey、Save等等一些静态方法。
  1.Create操作
None.gif User objUser  =   new  User();
None.gifobjUser.Name 
=   " jailu " ;
None.gifobjUser.Password 
=   " 123456789 " ;
None.gif
None.gifobjUser.Create();

  2.Read操作
None.gif User objUser  =  User.Find( 1 );     // 检索主键ID为1的User对象
None.gif
string  strName  =  objUser.Name;
None.gif
string  strPassword  =  objUser.Password;

  3.Update操作
None.gif User objUser  =  User.Find( 1 );     // 检索主键ID为1的User对象
None.gif
objUser.Name  =   " EmmaLee " ;
None.gifobjUser.Password 
=   " 987654321 " ;
None.gif
None.gifobjUser.Update();

  4.Delete操作
None.gif  User objUser  =  User.Find( 1 );     // 检索主键ID为1的User对象
None.gif

None.gifobjUser.Delete();

  5.完整的User类代码:

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Collections;
None.gif
None.gif
using  Castle.ActiveRecord;
None.gif
using  Castle.ActiveRecord.Queries;
None.gif
None.gif
namespace  CastleTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//为User类添加特性,其实就是告诉ActiveRecord,User类所对应的数据库中的数据表名为Users
InBlock.gif
    [ActiveRecord("Users")]
InBlock.gif    
public class User : ActiveRecordBase
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private int _id;
InBlock.gif        
private string _name;
InBlock.gif        
private string _password;
InBlock.gif
InBlock.gif        
//[PrimaryKey]特性指定Id作为主键
InBlock.gif        
//用PrimaryKeyType.Identity来说明了主键的类型为自增型的
InBlock.gif
        [PrimaryKey(PrimaryKeyType.Identity, "ID")]
InBlock.gif        
public int Id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _id = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [Property(
"LoginName")]
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _name = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//若属性名与列名一致可省略不写
InBlock.gif
        [Property]
InBlock.gif        
public string Password
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _password; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _password = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void DeleteAll()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            DeleteAll(
typeof(User));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static IList FindAll()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (IList)FindAll(typeof(User));
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static User Find(int id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return (User)FindByPrimaryKey(typeof(User), id);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 根据LogonName查询User对象,在测试HQL查询时用到
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="LogonName">登录名</param>
ExpandedSubBlockEnd.gif        
/// <returns>User对象数组</returns>

InBlock.gif        public static User[] GetUsersByLogonName(string LogonName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SimpleQuery query 
= new SimpleQuery(typeof(User),@"from User user where user.Name = ?",LogonName);
InBlock.gif
InBlock.gif            
return (User[])ExecuteQuery(query);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

六、表示层调用:
None.gif private   void  button1_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//初始化,获取连接字符串等信息
InBlock.gif
    IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord"as IConfigurationSource;
InBlock.gif    ActiveRecordStarter.Initialize(source, 
typeof(User));
InBlock.gif
InBlock.gif    User objUser 
= new User();
InBlock.gif    objUser.Name 
= "jailu";
InBlock.gif    objUser.Password 
= "123456789";
InBlock.gif
InBlock.gif    objUser.Create();
ExpandedBlockEnd.gif}

至此,ActiveRecord的初步接触就算是完成了。

转载于:https://www.cnblogs.com/jailu/archive/2006/08/29/488957.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值