Skip to content

NHibernate.DependencyInjection

ranzlee edited this page Apr 9, 2013 · 18 revisions

Target Framework = 3.5, 4.0, 4.5

NHibernate = 3.3.3.4000

Register the bytecode provider with NHibernate
Initializer.RegisterBytecodeProvider(new EntityInjector());

Implement a IEntityInjector. Anytime NHibernate needs to create an entity (or proxy), a call to GetConstructorParameters is made. Evaluate the type and return the constructor arguments, or null for a parameterless constructor.

public class EntityInjector : IEntityInjector                                                      
{                                                                                                  
    public object[] GetConstructorParameters(System.Type type)                                     
    {                                                                                              
        if (type == typeof(DependencyInjectionCat)) return new object[] {new CatDependency()};       
        return null;                                                                               
    }                                                                                              
}