Dependency injection with NHibernate objects

前端 未结 3 879
北海茫月
北海茫月 2020-12-05 16:08

I am wondering how to tell NHibernate to resolve dependencies on my POCO domain objects.

I figured out that methods like CalculateOrderTax should be in the Domain ob

3条回答
  •  生来不讨喜
    2020-12-05 17:02

    I've been using interceptors for similar tasks:

    An interceptor that modifies loaded entities:

    public class MyInterceptor : EmptyInterceptor
    {
        public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)
        {
            return InjectDependencies(entity as MyEntity);
        }
    }
    

    Associate it with a session:

    nhSessionFactory.OpenSession(myInterceptor);
    

    I've also read somewhere that there would be better support for custom constructor injection in the upcoming 2.1 release but I can't seem to find the reference right now.

提交回复
热议问题