NHibernate对象的依赖注入 [英] Dependency injection with NHibernate objects

查看:150
本文介绍了NHibernate对象的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何告诉NHibernate来解决对我的POCO域对象的依赖。



我发现,像CalculateOrderTax这样的方法应该在Domain对象中,因为它们编码域特定业务规则。但是一旦我有两个我违反SRP。



将这些方法提取到Strategy类是没有问题的,但我不知道如何使NHibernate加载它们。



在将存储库中的对象列表移交到更高版本之前,循环访问存储库中的对象列表似乎不是一个很好的解决方案,以便基于get / set的Dependecy注入



我现在也使用Castle Windsor进行我的Depency注入。

解决方案

我一直在使用拦截器进行类似的任务:



修改加载实体的拦截器:

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

将其与会话关联:

  nhSessionFactory.OpenSession(myInterceptor); 

我也读过某个地方,即将推出的2.1版本中将会更好地支持自定义构造函数注入但我现在似乎找不到参考。


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 object because they encode domain specific business rules. But once I have two of those I am violating SRP.

It would be no problem to extract those methods to Strategy classes, but I wonder how to make NHibernate load those.

It doesn't seem like a good solution to loop through a list of objects in the repository to do get/set based Dependecy injection before handing the object off to the higher layers.

I am also using Castle Windsor for my Depency injection right now.

解决方案

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.

这篇关于NHibernate对象的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆