Castle.ActiveRecord的ProxyFactory配置

简介:

  前后差不多两年没使用过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进行某些控制,暂时没有时间研究,期待有研究过的朋友分享,谢谢。





本文转自 beniao 51CTO博客,原文链接:http://blog.51cto.com/beniao/437845,如需转载请自行联系原作者

目录
相关文章
|
15天前
SpringCloud启动Consider defining a bean of type ‘org.springframework.web.client.RestTemplate‘ in your
SpringCloud启动Consider defining a bean of type ‘org.springframework.web.client.RestTemplate‘ in your
7 1
|
6月前
|
Java 数据库连接 mybatis
flowable项目启动项目报错:Consider defining a bean of type ‘org.mybatis.spring.SqlSessionTemplate‘ in ……
flowable项目启动项目报错:Consider defining a bean of type ‘org.mybatis.spring.SqlSessionTemplate‘ in ……
128 0
|
8月前
|
应用服务中间件 Linux Windows
tomcat部署web项目报错:Could not initialize class sun.awt.X11GraphicsEnvironment
tomcat部署web项目报错:Could not initialize class sun.awt.X11GraphicsEnvironment
186 0
|
12月前
|
Java 测试技术 Spring
Configuration problem: Failed to import bean definitions from URL location classpath:spring-mvc.xml
Configuration problem: Failed to import bean definitions from URL location classpath:spring-mvc.xml
607 0
|
Java Spring
SpringCloud - allowBeanDefinitionOverriding(spring.main.allow-bean-definition-overriding)分析
SpringCloud - allowBeanDefinitionOverriding(spring.main.allow-bean-definition-overriding)分析
620 0
|
Java 应用服务中间件 Apache
webservice开发不得不知的细节,Error creating bean with name ‘org.apache.cxf.jaxws.EndpointImpl---1987203924‘
webservice开发不得不知的细节,Error creating bean with name ‘org.apache.cxf.jaxws.EndpointImpl---1987203924‘
125 0
【错误记录】集成 Tinker 热修复报错 ( No such property: variantConfiguration for class: .ApplicationVariantData )
【错误记录】集成 Tinker 热修复报错 ( No such property: variantConfiguration for class: .ApplicationVariantData )
668 0
【错误记录】集成 Tinker 热修复报错 ( No such property: variantConfiguration for class: .ApplicationVariantData )