]]>By: Jonathanhttp://lostechies.com/jimmybogard/2008/12/15/making-frameworks-container-aware/#comment-1079
JonathanMon, 15 Dec 2008 11:50:25 +0000/blogs/jimmy_bogard/archive/2008/12/14/making-frameworks-container-aware.aspx#comment-1079We've tried to avoid listeners for object creation because things can get complicated quickly - as you have discovered.
NHibernate isn't the most IoC friendly framework. What we ended up doing was creating a private field in our repositories that looked like this:
private Action<T> initialize;
This private field served as a callback that populated our object instance - typically through setter injection (ugh!)
We used setter injection rather than constructor injection for several reasons, but the most important was to allow NHibernate to create a proxy object for us so that we gained the benefits of deferred query execution.
Here is our "load" method on our repository:
public virtual T FindById(int id)
{
var loaded = (T)this.session.Load(this.type, id);
this.initialize(loaded);
return loaded;
}
In this instance "T" would be something like IContact, IAddress, IUser, etc. whereas "this.type" would be a concrete implementation for the respective interface.
We are actually performing dependency injection on our repositories to inject the session, type, and initialize objects.
In this manner we make NHibernate a bit more IoC friendly.
We’ve tried to avoid listeners for object creation because things can get complicated quickly – as you have discovered.

NHibernate isn’t the most IoC friendly framework. What we ended up doing was creating a private field in our repositories that looked like this:

private Action initialize;

This private field served as a callback that populated our object instance – typically through setter injection (ugh!)

We used setter injection rather than constructor injection for several reasons, but the most important was to allow NHibernate to create a proxy object for us so that we gained the benefits of deferred query execution.