使用 NHibernate 对象进行依赖注入

编程入门 行业动态 更新时间:2024-10-23 21:34:39
本文介绍了使用 NHibernate 对象进行依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何告诉 NHibernate 解决对我的 POCO 域对象的依赖关系.

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

我发现CalculateOrderTax 之类的方法应该在Domain 对象中,因为它们对特定于域的业务规则进行编码.但是一旦我拥有其中的两个,我就违反了 SRP.

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.

将这些方法提取到 Strategy 类没有问题,但我想知道如何让 NHibernate 加载这些方法.

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

在将对象移交给更高层之前,循环遍历存储库中的对象列表以执行基于 get/set 的依赖注入似乎不是一个好的解决方案.

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.

我现在也在使用 Castle Windsor 进行依赖注入.

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);
    }
}

将其与会话相关联:

nhSessionFactory.OpenSession(myInterceptor);

我还在某处读到在即将发布的 2.1 版本中会更好地支持自定义构造函数注入,但我现在似乎找不到参考.

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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 16:17:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/808790.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!