Try fast search NHibernate

24 May 2009

NHibernate IoC integration

Do you remember this post ?

As you can see you can use Dependency Injection even for entities, but what about all others classes needed by NHibernate ?

Can you inject something in a custom Dialect or in a custom UserType or UserCollectionType or Listener and all others extensions points ?

Sure you can ;)

NHibernate 2.1.0Alpha3, the fresh released today, has IObjectsFactory. As the ProxyFactoryFactory, the ReflectionOptimizer even the ObjectsFactory is a responsibility of the ByteCodeProvider.

To be short… an implementation of a IUserType now can look like this:

public class InjectableStringUserType : IUserType
private readonly IDelimiter delimiter;

public InjectableStringUserType(IDelimiter delimiter)
this.delimiter = delimiter;

The implementation of IPostInsertEventListener now can look like this:

public class YourPostInsertListener : IPostInsertEventListener
private readonly IPersistentAuditor auditor;

public YourPostInsertListener(IPersistentAuditor auditor)
this.auditor = auditor;

public void OnPostInsert(PostInsertEvent @event)

If you want use Dependency-Injection for both entities and all others NH stuff, in uNhAddIns you can find two full implementation for Castle and Spring.

Enjoy NHibernate injectability.

P.S. Part of it (tests), was a live implementation before start the Alt.NET VAN today.


  1. Any examples how to configure it?

    Environment.BytecodeProvider = new EnhancedBytecode(container);

    Or is there a way to do it from hibernate.cfg.xml?

  2. So far only programmatic conf..
    Btw if we will enable the configuration trough XML conf. it will be available only inside app.config or web.config because, as you saw, the Bytecode provider must be set before call cfg.Configure()

  3. I am familiar with (and like) Unity. Any plans (or chance of) support for that?

  4. Sure it is only a matter of time. If you have time to spent in it you are welcome. The first step is the proxy of the session (SessionWrapper of uNhAddIns).

  5. I just spotted this in the uNhAddIns Bytecodeprovider:
    // A better place for this classes is Spring.Data.NHibernate
    Does that implementation differ from that one in the NHibernate trunk you talk about here:
    Anyways, thanks for the support :)

  6. Sure they are different. The part on NH is only the proxy provider (using Spring AOP). What you can see in uNhAddIns is the entirely Bytecode (full integration with Dependency-Injection and AOP stuff)

  7. Ah... about "Spring.Data.NHibernate" is because I hope that Mark will implement and maintain it directly in Spring. Waiting Spring's team you can use uNhAddIns or copy&paste the code you need.

  8. Thanks fabio. I also hope that those things will be in Spring.Data.NHibernate in the future.

  9. I posted an implementation with Autofac here:

  10. Thanks to Fabio for donating his code. You can find the ByteCodeProvider in the new NH 2.1 integration package coming with Spring 1.3RC at