本文介绍了我可以在NHibernate中替换对Activator.CreateInstance()的调用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以替换对NHibernate 2.0.1GA中使用的Activator.CreateInstance()的调用来构造实体?理想情况下,我想将其替换为StructureMap.ObjectFactory.GetInstance().

Is there a way to replace the call to Activator.CreateInstance() used inside NHibernate 2.0.1GA to construct the entities? Ideally I'd like to replace it with StructureMap.ObjectFactory.GetInstance().

推荐答案

使用构造函数注入很难做到这一点. NHibernate内部构件可能需要创建一个从您的域类继承而来的代理对象,并带有延迟加载代码等.因此,据我所知,没有简单的方法可以覆盖对象的构造.

You can't do this easily with constructor injection. The NHibernate internals may need to create a proxy object inherited from your domain class with Lazy loading code etc. sprinkled in there, so, as far as I know, there's no simple option to override the construction of your object.

通过编写一个通过属性设置器注入为您构建对象的拦截器,您 可以很容易地与NHibernate进行依赖注入.有一个此处.该示例将Windsor用作DI容器,但是您当然可以使用StructureMap做同样的事情-这里是有关StructureMap的相关文档-查找有关将Setter注入应用于现有对象(BuildUp)"的部分.

You can get dependency injection working with NHibernate fairly easily though, by writing an interceptor that will build up the object for you via property setter injection. There is an example of this here. That example is using Windsor as the DI container, but you can do the same thing with StructureMap of course - here are the relevant docs for StructureMap - look for the section on "Applying Setter Injection to an Existing Object (BuildUp)".

注意事项:将依赖项注入到域模型中通常被视为设计问题的征兆,许多人避免这样做,因此在开始编码之前,请确保这确实是您想要做的事情!

Caveat: injecting dependencies into your domain model is often seen as a symptom of a design problem, and many people avoid doing this, so be sure this is really what you want to do before you start coding it!

这篇关于我可以在NHibernate中替换对Activator.CreateInstance()的调用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:19