NHibernate + Unity Interception = NHibernate.Bytecode.Unity Provider
By: Pablo Daniel González
Tags: Software Development, Innovacion, Technology

Hi! Because we have updated the Unity.Interception.Serialization component, we can’t stop updating the btycode provider for Nhibernate.

So, we have published the updated component on nuget.org. The aim of this is the component versioning and dependencies it has.

We also added some utilities, first, the proxy resolution use the container, second, support for resolve multi session factory through the container and configuration extension for this one.

If you only want use the component for lazy initialization, the configuration is that:

C#

NHibernate.Bytecode.Unity.Tests.ProxyInterface.CustomProxyFactoryFactory, NHibernate.Bytecode.Unity.Tests

Using the container for resolving proxy enabling us to add strategies and policies for entities and proxy resolutions. For example modify the behavior when these are in WCF operation context or maybe trace info, add metadata, of course DI, etc. The only concern is that Nhibernate proxies for entities which implement a interface don’t use the original class but a object class with additional interfaces.

You should use Register and Resolve methods this way:

C#

container.AddNewExtension(); //Add extension, the extension only can registered on one only container. cotainer.RegisterType(“EntityName”); //For entities with virtual methods.

cotainer.RegisterType(“EntityName”); //For entities which expose properties through a interface.

cotainer.Resolve(“EntityName”);

cotainer.Resolve(“EntityName”);

When using registertype method can do all things you usually do because the bytecode provider adds the interception support. You must respect the BuildKey (Type and Name) for avoid unexpected behaviors.

Important: The extension only can registered on one only container, if the extension is registered in various containers, the bytecode provider going to use the last container.

Note: You can use the bytecode provider without register the extension.

Remember: You don’t need registering the interception extension, because it is automatically added by NHibernateUnityExtension.

The support for resolve multi session factory through the container is a simple thing.

Example:

C#

container.AddNewExtension(); //Add configurations for session factories

container.RegisterInstance(NHconfiguration,”ConnectionStringName”)

container.RegisterInstance(NHconfiguration2,”ConnectionStringName2") //Resolve de session factories.

container.Resolve(“ConnectionStringName”)

container.Resolve(“ConnectionStringName2”)

You can use lazy build for ISessionFactory to delay building until it is requiered:

C#

container.AddExtension(new NHibernateUnityExtension(lazyBuildSessionfactory:true)); //You can modify configuration while don’t resolve ISessionFactory

var configuration = container.Resolve(“ConnectionStringName”);

The configuration for the component is the next:

<unity xmlns=”http://schemas.microsoft.com/practices/2010/unity">

<sectionExtension type=”NHibernate.Unity.Configuration.NHibernateUnityConfigurationExtension, NHibernate.Unity.Configuration” />

<container>

<extension type=”NHibernate”/> <!−−Adds the extension to container−−>

</container>

</unity>

Or:

<container>

<NHibernate/> <!−−Adds the extension to container−−>

</container>

To delay building of ISessionFactory until it is requiered:

<container>

<register type=”NHibernate”><!−−Register constructor policy for extension−−>

<constructor>

<param name=”lazyBuildSessionfactory” value=”true” type=”bool”/>

</constructor>

</register>

<NHibernate/> <!−−You must use this way−−>

</container>

The most important thing, how to configure the NHibernate Configuration:

<container>

<NHibernate>

<sessionFactories>

<sessionFactory connectionStringName=”ConnectionStringNameFromAppConfig” file=”MsSql2005Dialect.config”>

<mappings>

<mapping assembly=”mappingAssembly”/>

<mapping assembly=”AssemblyWithSomeResources” resource=”MapClass.hbm.xml”/>

<mapping class=”Mappings.SQL2005.Entity, Mappings”/>

<mapping directory=”DirectoryWithMappingFiles”/>

<mapping file=”CommonClasses.hbm.xml”/>

<mapping uri=”http://getwhituriformat/classes.hbm.xml”/>

</mappings>

</sessionFactory>

<sessionFactory connectionStringName=”ConnectionStringName” file=”MySQLDialect.config”>

<mappings>

<mapping file=”CommonClasses.hbm.xml”/>

<mapping file=”MyClassForMySQL.hbm.xml”/>

</mappings>

</sessionFactory>

</sessionFactories>

</NHibernate>

</container>

I hope everything will be useful for you. Feel free to use as you please or to modify it. Be sure to contact me for any mistakes.

See you soon!!

License:

The component is achieved by these licenses: MIT and LGPL-3.0

The component was released under the LGPL license. But I recommend you read the explanations of our open source projects, which allows you to use another set of licenses. You can see that here.

Update: We are removed the old sources.

Update: We move forward on a more complete solution component to support dependency injection in the domain objects of NHibernate but we could not finalize development due to lack of time and funding. This would have been the component that allow most complete and transparent integration between Unity and NHibernate.

Update: 29/03/2015 Source code available on GitHub

NHibernate + Unity Interception = NHibernate.Bytecode.Unity Provider por Pablo González se distribuye bajo una Licencia Creative Commons Atribución-CompartirIgual 4.0 Internacional.