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.
Any examples how to configure it?ReplyDelete
Environment.BytecodeProvider = new EnhancedBytecode(container);
Or is there a way to do it from hibernate.cfg.xml?
So far only programmatic conf..ReplyDelete
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()
I am familiar with (and like) Unity. Any plans (or chance of) support for that?ReplyDelete
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).ReplyDelete
I just spotted this in the uNhAddIns Bytecodeprovider:ReplyDelete
// 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: http://nhforge.org/blogs/nhibernate/archive/2009/03/03/nhibernate-with-spring-net.aspx
Anyways, thanks for the spring.net support :)
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)ReplyDelete
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.ReplyDelete
Thanks fabio. I also hope that those things will be in Spring.Data.NHibernate in the future.ReplyDelete
I posted an implementation with Autofac here:ReplyDelete
well done Chad.ReplyDelete
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 http://www.springframework.netReplyDelete