Castle.ActiveRecord的ProxyFactory配置
2010-11-24 13:32
405 查看
前后差不多两年没使用过Castle.ActiveRecord做ORM了,也近两年没有关注Castle的版本变化了,最近关注了发现很多地方都改变了。今天在一个现有的小项目中尝试添加Castle.ActiveRecord来做数据持久化操作,以前不需要做ProxyFactory配置,现在必须要ProxyFactory配置。使用到了NHibernate.ByteCode.Castle.dll组件。
ActoiveRecord的配置文件有了明显的变化,需要配置ProxyFactory才能完成持久化操纵,详细配置如下:
<activerecord>
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=SQL05;Initial Catalog=testdb;uid=sa;pwd=12345;" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
</config>
</activerecord>
其他的使用基本上没有变化,下面是一个ORM的对象配置。
[ActiveRecord("UserInfo")]
public class UserInfo : ActiveRecordBase<UserInfo>
{
[PrimaryKey(PrimaryKeyType.Identity, "ID")]
public int ID { get; set; }
[Property("Sex", Length = 50)]
public string Sex { get; set; }
[Property("Name", NotNull = true)]
public string Name { get; set; }
}
初始化配置也还是和以前的版本使用方式一样的,支持单个对象、多个对象、单个程序集、多个程序集等等不同方式的关系映射对象进行初始化。
protected void Application_Start(object sender, EventArgs e)
{
IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
//ActiveRecordStarter.Initialize(source, typeof(Entity.UserInfo));
ActiveRecordStarter.Initialize(Assembly.Load("Entity"), source);
}
由于关系映射对象继承于ActiveRecordBase<T>泛型版本,因此可以直接基于关系映射对象使用数据持久化操纵行为,和老版本的使用方式是一样的。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UserInfo info = new UserInfo();
info.Name = "zhangsan";
info.Sex = "男";
//插入info数据到数据库中所映射的关系表
info.Create();
}
}
至于ProxyFactory的具体作用暂时没有深入研究,也不知道AR为什么会在新版本中添加这个功能,我想应该是想通过NHibernate对Castle的AR进行某些控制,暂时没有时间研究,期待有研究过的朋友分享,谢谢。
ActoiveRecord的配置文件有了明显的变化,需要配置ProxyFactory才能完成持久化操纵,详细配置如下:
<activerecord>
<config>
<add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
<add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="connection.connection_string" value="Data Source=SQL05;Initial Catalog=testdb;uid=sa;pwd=12345;" />
<add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
</config>
</activerecord>
其他的使用基本上没有变化,下面是一个ORM的对象配置。
[ActiveRecord("UserInfo")]
public class UserInfo : ActiveRecordBase<UserInfo>
{
[PrimaryKey(PrimaryKeyType.Identity, "ID")]
public int ID { get; set; }
[Property("Sex", Length = 50)]
public string Sex { get; set; }
[Property("Name", NotNull = true)]
public string Name { get; set; }
}
初始化配置也还是和以前的版本使用方式一样的,支持单个对象、多个对象、单个程序集、多个程序集等等不同方式的关系映射对象进行初始化。
protected void Application_Start(object sender, EventArgs e)
{
IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
//ActiveRecordStarter.Initialize(source, typeof(Entity.UserInfo));
ActiveRecordStarter.Initialize(Assembly.Load("Entity"), source);
}
由于关系映射对象继承于ActiveRecordBase<T>泛型版本,因此可以直接基于关系映射对象使用数据持久化操纵行为,和老版本的使用方式是一样的。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UserInfo info = new UserInfo();
info.Name = "zhangsan";
info.Sex = "男";
//插入info数据到数据库中所映射的关系表
info.Create();
}
}
至于ProxyFactory的具体作用暂时没有深入研究,也不知道AR为什么会在新版本中添加这个功能,我想应该是想通过NHibernate对Castle的AR进行某些控制,暂时没有时间研究,期待有研究过的朋友分享,谢谢。
相关文章推荐
- C#.Net Castle ActiveRecord学习实践(2):构建配置信息
- Castle Activerecord多数据库如何配置详解
- Castle.ActiveRecord的初始化配置补充及配置封装的解决方案
- Castle ActiveRecord学习实践(2):构建配置信息
- 记录Castle ActiveRecord访问Sqlite的配置
- Castle ActiveRecord学习实践(2):构建配置信息
- Castle ActiveRecord学习实践(2):构建配置信息
- Castle ActiveRecord 使用动态填写参数方式配置数据库连接
- Castle.ActiveRecord 多数据库的配置(自家用)
- step by step castle ActiveRecord示例项目详读之配置信息和初始化
- Castle Active Record for .NET2.0快速入门示例
- Castle Active Record for .NET2.0快速入门示例
- Castle ActiveRecord学习实践(6):延迟加载和使用Where子句
- Castle ActiveRecord学习笔记四:各种映射
- Castle.ActiveRecord 多对多关系 引发的错误处理
- Castle ActiveRecord学习实践(6):延迟加载和使用Where子句
- C#.Net Castle ActiveRecord学习实践(3):映射基础
- ObjectQuery for Castle.ActiveRecord 使用指南(1):From语句
- Castle ActiveRecord框架学习(一)
- Castle ActiveRecord Hands On Lab(1):基本数据访问