Question

I would like to use prototype interceptors instead of a singleton interceptor so each session would get a new interceptor instance.

I looked into the HibernateTransactionManager Class and I think "EntityInterceptorObjectName" and "ObjectFactory" are the properties I have to set.

While EntityInterceptorObjectName is pretty obvious, I have no clue how to reference the ObjectFactory when it's the "mother of the ObjectFactories", e.g. the same ObjectFactory which creates the AppContext/the HibernateTransactionManager.

The relevant part of the config:

   <object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate20">
    <property name="DbProvider" ref="DbProvider"/>
    <property name="SessionFactory" ref="SessionFactory"/>
    <!-- the name of my non-Singleton EntityInterceptor-->
     <property name="EntityInterceptorObjectName" value="MyPrototypeEntityInterceptor" />
    <!-- What should I put as ref here? -->
    <property name="ObjectFactory" ref="" />
  </object>

  <object id="MyPrototypeEntityInterceptor" type="Hib.EntityInterceptor, Hib.Interceptors" singleton="false">
  </object>  
Was it helpful?

Solution

As i understand it you can use your own Implementation of IObjectFactory creating the objects you want according to the given name. If you want to be able to use the objects defined within your Spring-ApplicationContext you can also implement IObjectFactoryAware to connect your ObjectFactory to your Spring IoC Container.

This should enable you to do whatever you want. Create new instances of any class and fill its properties with objects from your IoC container (for example SessionFactory, ...).

public class MyObjectFactoryAware : IObjectFactoryAware, IObjectFactory
{
   //this property will be set by the IoC container
   ObjectFactory {get; set;}

   //IObjectFactory implementation
   virtual object GetObject(string objectName)
   {
      MyEntityInterceptor interceptor = new MyEntityInterceptor();
      interceptor.SessionFactory = this.ObjectFactory.getObject("SessionFactory");
      return interceptor;

      //or of course if you just need it for wireing simply:
      //return this.ObjectFactory.getObject(objectName);
   }   
}

and the mapping:

<object id="HibernateTransactionManager" type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate20">
  <property name="DbProvider" ref="DbProvider"/>
  <property name="SessionFactory" ref="SessionFactory"/>
  <!-- the name of my non-Singleton EntityInterceptor-->
  <property name="EntityInterceptorObjectName" value="MyPrototypeEntityInterceptor" />
  <!-- and the name of the objectFactory -->
  <property name="ObjectFactory" ref="MyObjectFactory" />
</object>

<object id="MyObjectFactory" type="Hib.MyObjectFactoryAware, Hib.Interceptors">
</object>

<object id="MyPrototypeEntityInterceptor" type="Hib.EntityInterceptor, Hib.Interceptors" singleton="false">
</object>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top