Posterous theme by Cory Watilo

Inject dependencies on NHibernate domain objects with Unity (A unity BytecodeProvider)

Having just dove head first into the NHibernate world, I'm learning all kinds of "stuff."  I say stuff that way because I don't know if I like it or not yet.

Any case, I ran into the situation where I wanted domain objects created from NHibernate to be automatically injected with objects from the DI container I was using.  Specifically, Unity since I'm such a Microsoft whore currently.

The solution is to create a custom NHibernate BytecodeProvider so that NHibernate will resolve objects with the container instead.  There are prebuilt solutions here but nothing specifically for Unity.  After battling with the API a bit, which took longer than I thought it would, I successfully made a unity specific provider.  Check it out:

https://github.com/adamhathcock/UnityBytecodeProvider