[LogonID] [int] IDENTITY (1, 1) NOT NULL ,
[LogonName] [varchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
[Password] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[EmailAddress] [varchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
[LastLogon] [datetime] NULL
) ON [PRIMARY]
GO
三.编写User实体类
{
//
}
为User类添加特性,其实就是告诉ActiveRecord,User类所对应的数据库中的数据表名为Users
public class User : ActiveRecordBase
{
//
}
下面我们的工作就是为实体类添加属性
public class User : ActiveRecordBase
{
private int _id;
private string _name;
private string _password;
private string _emailAddress;
private DateTime _lastLogon;
[PrimaryKey(PrimaryKeyType.Identity, "LogonID")]
public int Id
{
get { return _id; }
set { _id = value; }
}
[Property("LogonName")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[Property("Password")]
public string Password
{
get { return _password; }
set { _password = value; }
}
[Property("EmailAddress")]
public string Address
{
get { return _emailAddress; }
set { _emailAddress = value; }
}
[Property("LastLogon")]
public DateTime LastLogon
{
get { return _lastLogon; }
set {_lastLogon = value; }
}
}
大家可能注意到了,每一个属性上面都加上了特性[Property()]。简单的说明一下,这里用[PrimaryKey]特性指定Id作为主键,并且说明了主键的类型为自增型的,用PrimaryKeyType.Identity来说明,在后续文章中我会详细说明的。如果属性名和字段名一致,[Property()]中可以为空,也可以写上字段的名字。
public class User : ActiveRecordBase
{
//……
public static void DeleteAll()
{
DeleteAll( typeof(User) );
}
public static IList FindAll()
{
return (IList) FindAll( typeof(User) );
}
public static User Find(int id)
{
return (User) FindByPrimaryKey( typeof(User), id );
}
}
using System.Collections;
using Castle.ActiveRecord;
namespace ARDemo
{
/// <summary>
/// User 的摘要说明。
/// </summary>
[ActiveRecord("Users")]
public class User : ActiveRecordBase
{
private int _id;
private string _name;
private string _password;
private string _emailAddress;
private DateTime _lastLogon;
[PrimaryKey(PrimaryKeyType.Identity, "LogonID")]
public int Id
{
get { return _id; }
set { _id = value; }
}
[Property("LogonName")]
public string Name
{
get { return _name; }
set { _name = value; }
}
[Property("Password")]
public string Password
{
get { return _password; }
set { _password = value; }
}
[Property("EmailAddress")]
public string Address
{
get { return _emailAddress; }
set { _emailAddress = value; }
}
[Property("LastLogon")]
public DateTime LastLogon
{
get { return _lastLogon; }
set {_lastLogon = value; }
}
public static void DeleteAll()
{
DeleteAll( typeof(User) );
}
public static IList FindAll()
{
return (IList) FindAll( typeof(User) );
}
public static User Find(int id)
{
return (User) FindByPrimaryKey( typeof(User), id );
}
}
}
<configuration>
<configSections>
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
</configSections>
<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="UID=sa;Password=sa;Initial Catalog=ARDemo;Data Source=." />
</config>
</activerecord>
</configuration>
用过NHibernate的朋友一定会对这段配置代码很熟悉,没错,因为ActiveRecord在底层封装了NHibernate,所以这里的配置跟使用NHibernate时的配置一样,同样是指定了数据源驱动,连接字符串等信息。如果使用了配置文件在代码中只要这样去初始化就可以了
ActiveRecordStarter.Initialize( source, typeof(User) );
我们也可以不使用配置文件,而使用代码指定的方式,但是由于这种方式相当于硬编码了,不大推荐大家使用这种方式:
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", "UID=sa;Password=19811218;Initial Catalog=ARDemo;Data Source=.");
source.Add( typeof(ActiveRecordBase), properties );
ActiveRecordStarter.Initialize( source, typeof(User) );
public void AddUser()
{
User user = new User();
user.Name = "Terrylee";
user.Password = "aaa";
user.Address = "[email]lhj_cauc@163.com[/email]";
user.LastLogon = DateTime.Now;
user.Create();
}
是不是非常简单?我们甚至都没有写过Create()方法,它直接从ActiveRecordBase类继承。我们所做的只是创建这样一个User对象,然后调用它的方法就可以了。
public void FildAll()
{
IList list = User.FindAll();
Assert.IsNotNull(list);
int actual = list.Count;
int expected = 2;
Assert.AreEqual(expected,actual);
}
3.查询某一个指定Id的User对象
public void Fild()
{
int id = 5;
User actual = User.Find(id);
Assert.IsNotNull(actual);
Assert.AreEqual("Terrylee",actual.Name);
Assert.AreEqual("aaa",actual.Password);
}
4.修改User对象
public void UpdateUser()
{
User user = new User();
user.Id = 5;
user.Name = "Aero";
user.Password = "aaa";
user.Address = "[email]chwkai@163.com[/email]";
user.LastLogon = DateTime.Now;
user.Update();
}
5.删除User对象
public void DeleteUser()
{
User user = new User();
user.Id = 7;
user.Delete();
}
6.删除所有的User对象
public void DeleteAll()
{
User.DeleteAll();
}
可以看到,整个过程非常的简洁简单,没有一点多余复杂的代码,相信你已经开始体会到了ActiveRecord的魅力了。唯一有一点你会感到不舒服的是已经有了数据库表还需要手工编写实体类代码,这个不用担心,ActiveRecord已经为我们提供了代码生成工具ActiveRecord Generator。
// Generated by ActiveRecord Generator
//
//
namespace ARDemo
{
using Castle.ActiveRecord;
[ActiveRecord("Users")]
public class User : ActiveRecordBase
{
private int _logonID;
private string _logonName;
private string _password;
private string _emailAddress;
private System.DateTime _lastLogon;
[PrimaryKey(PrimaryKeyType.Native)]
public int LogonID
{
get
{
return this._logonID;
}
set
{
this._logonID = value;
}
}
[Property()]
public string LogonName
{
get
{
return this._logonName;
}
set
{
this._logonName = value;
}
}
[Property()]
public string Password
{
get
{
return this._password;
}
set
{
this._password = value;
}
}
[Property()]
public string EmailAddress
{
get
{
return this._emailAddress;
}
set
{
this._emailAddress = value;
}
}
[Property()]
public System.DateTime LastLogon
{
get
{
return this._lastLogon;
}
set
{
this._lastLogon = value;
}
}
public static void DeleteAll()
{
ActiveRecordBase.DeleteAll(typeof(User));
}
public static User[] FindAll()
{
return ((User[])(ActiveRecordBase.FindAll(typeof(User))));
}
}
}
大家还应该注意的一点是生成One-Many/Many-One等关系的实体类文件时可能会出现一些问题,需要对生成的代码手工改动。最后希望和研究Castle的朋友能够多多交流!