有这段文字的惟一原因是因为spring.net尚远远没有完成。因此,借鉴spring (java)的LocalSessionFactoryObject,我们在.net中创建一个LocalSessionFactoryObject
注:在Spring.Net的开发计划中,第一个版本仅会完成aop和ioc功能,即现在在cvs上大家看到的Spring.Context,Spring.AOP,Spring.Core三个项目。下面的Spring.Data是我自行创建的,用来迁移一些项目中所必须用到的东西。
LocalSessionFactoryObject的作用
LocalSessionFactoryObject将NHibernate的配置整合到Spring中,因此,你不在需要app.config中配置nhibernate的属性,也无需将hbm xml文件设置为嵌入
LocalSessionFactoryObject有两个重要属性。MappingResources是一个IList,是一系列hbm文件的列表,而HibernateProperties则是一个IDictonary,存放NHibernate的设置
<object name="mySessionFactory" class="Spring.Data.Hibernate.LocalSessionFactory,Spring.Data">
<property name="MappingResources">
<list>
<value>customer.hbm.xml</value>
<value>color.hbm.xml</value>
</list>
</property>
<property name="HibernateProperties">
<props>
<prop key="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</prop>
<prop key="hibernate.dialect">NHibernate.Dialect.MsSql2000Dialect</prop>
<prop key="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</prop>
<prop key="hibernate.connection.connection_string">Server=localhost;initial catalog=mis;User ID=sa;Password=;Min Pool Size=2</prop>
</props>
</property>
</object>
LocalSessionFactoryObject实现了IFactoryObject接口,这意味着当从ioc获取LocalSessionFactory的实例是,将调用其GetObject方法,因此,大家对这段代码就不要有疑惑
ISessionFactory sessionFactory=(ISessionFactory)factory.GetObject("mySessionFactory");
LocalSessionFactory Object实现了IInitialObject接口,其AfterPropertiesSet方法在ioc配置属性后调用,非常简单
public void AfterPropertiesSet()
{
if (sessionFactory==null)
{
Configuration cfg=new Configuration();
cfg.AddProperties(hibernateProperties);
foreach(String hbm in mappingResources)
cfg.AddXmlFile(hbm);
sessionFactory=cfg.BuildSessionFactory();
}
}
下面是一段测试代码
IObjectFactory factory=new XmlObjectFactory(System.IO.File.OpenRead("applicationcontext.xml"));
Customer customer=new Customer();
customer.Name="jjx";
ISessionFactory sessionFactory=(ISessionFactory)factory.GetObject("mySessionFactory");
ISession session=sessionFactory.OpenSession();
ITransaction trans=session.BeginTransaction();
session.Save(customer);
trans.Commit();
注:在Spring.Net的开发计划中,第一个版本仅会完成aop和ioc功能,即现在在cvs上大家看到的Spring.Context,Spring.AOP,Spring.Core三个项目。下面的Spring.Data是我自行创建的,用来迁移一些项目中所必须用到的东西。
LocalSessionFactoryObject的作用
LocalSessionFactoryObject将NHibernate的配置整合到Spring中,因此,你不在需要app.config中配置nhibernate的属性,也无需将hbm xml文件设置为嵌入
LocalSessionFactoryObject有两个重要属性。MappingResources是一个IList,是一系列hbm文件的列表,而HibernateProperties则是一个IDictonary,存放NHibernate的设置
<object name="mySessionFactory" class="Spring.Data.Hibernate.LocalSessionFactory,Spring.Data">
<property name="MappingResources">
<list>
<value>customer.hbm.xml</value>
<value>color.hbm.xml</value>
</list>
</property>
<property name="HibernateProperties">
<props>
<prop key="hibernate.connection.provider">NHibernate.Connection.DriverConnectionProvider</prop>
<prop key="hibernate.dialect">NHibernate.Dialect.MsSql2000Dialect</prop>
<prop key="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</prop>
<prop key="hibernate.connection.connection_string">Server=localhost;initial catalog=mis;User ID=sa;Password=;Min Pool Size=2</prop>
</props>
</property>
</object>
LocalSessionFactoryObject实现了IFactoryObject接口,这意味着当从ioc获取LocalSessionFactory的实例是,将调用其GetObject方法,因此,大家对这段代码就不要有疑惑
ISessionFactory sessionFactory=(ISessionFactory)factory.GetObject("mySessionFactory");
LocalSessionFactory Object实现了IInitialObject接口,其AfterPropertiesSet方法在ioc配置属性后调用,非常简单
public void AfterPropertiesSet()
{
if (sessionFactory==null)
{
Configuration cfg=new Configuration();
cfg.AddProperties(hibernateProperties);
foreach(String hbm in mappingResources)
cfg.AddXmlFile(hbm);
sessionFactory=cfg.BuildSessionFactory();
}
}
下面是一段测试代码
IObjectFactory factory=new XmlObjectFactory(System.IO.File.OpenRead("applicationcontext.xml"));
Customer customer=new Customer();
customer.Name="jjx";
ISessionFactory sessionFactory=(ISessionFactory)factory.GetObject("mySessionFactory");
ISession session=sessionFactory.OpenSession();
ITransaction trans=session.BeginTransaction();
session.Save(customer);
trans.Commit();
session.Close();
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2010/12/20/1911864.html,如需转载请自行联系原作者