问题描述
我想知道如何告诉 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:关键词]
发布评论