将NHibernate POCO复制到DTO,而不会触发延迟加载或急切加载 [英] Copying NHibernate POCO to DTO without triggering lazy load or eager load

查看:137
本文介绍了将NHibernate POCO复制到DTO,而不会触发延迟加载或急切加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 NHibernate POCO 对象创建 DTO .问题在于POCO对象包含动态代理,不应将其复制到DTO. 我渴望加载所有我需要提前转移的集合和引用,我不希望NHibernate开始加载我没有提前加载的引用集合.

I need to create DTOs from NHibernate POCO objects. The problem is that the POCO objects contain dynamic proxies, which should not be copied to the DTO. I eager load all the collections and references I need to transfer in advance, I don't want NHibernate to start loading referenced collections which I did not load in advance.

关于SO的几个类似问题得到了以下答案:

Several similar questions on SO received answers which either:

  1. 建议Session.GetSessionImplementation().PersistenceContext.Unproxy();
  2. 建议关闭延迟加载.

在我的情况下,第一个建议是无关紧要的,因为根据我的理解,这会导致急于加载替换代理.实际上,它甚至不起作用-它不会删除对象中的代理. (任何解释为什么?)

In my case the first suggestion is irrelevant, as according to my understanding it causes eager loading to replace the proxies. In reality, it doesn't even work - it doesn't remove the proxies in my objects. (Any explanation why?)

第二个建议是,关闭延迟加载似乎会导致所有引用和集合都渴望加载,基本上是加载整个数据库.我的期望是,如果延迟加载已关闭,并且我尚未请求收集,则不会加载它. (我是否纠正NHibernate不提供这样的选择?)

The second suggestion, turning off lazy loading seems to cause all references and collections to eager load, basically loading the entire DB. My expectation was that if lazy loading is off, and I have not requested a collection, it will not be loaded. (Am I correct that NHibernate offers no such option?)

我正在使用具有流畅配置的NHibernate 3.3.1.

I am using NHibernate 3.3.1 with fluent configuration.

要重申我的主要问题,我需要创建清除代理的DTO,并从包含代理的POCO中复制它们,并且我不想在这些代理后面加载数据.

To reiterate my main question, I need to create DTOs clean of proxies, copied from POCOs which contain proxies, and I don't want to load the data behind those proxies.

任何有用的建议(包括示例代码和使用ValueInjecter/AutoMapper自动化过程)都将非常有帮助.

Any helpful suggestion which includes example code and automates the process with ValueInjecter / AutoMapper will be immensely helpful.

编辑#1:

按照罗杰·阿尔辛(Roger Alsing)的建议使用投影,我意识到我真正要寻找的是类似于ValueInjecter的基于约定的映射.这就是为什么.最初,将定义我的DTO与模型的POCO相同.这是由于庞大的代码库,这取决于在客户端项目上传输的现有POCO.

Following Roger Alsing's suggestion to use projections, I realized that what I'm actually looking for is a ValueInjecter-like convention based mapping. Here is why. Initially, my DTOs will be defined the same as the model's POCOs. This is due to a large code base which depends on the existing POCOs being transferred on the client-side project.

使用投影,我将必须指定必须复制的字段子集,并且该子集在每种情况下可能有所不同(理想情况下,DTO会有所不同).当应该有第二种选择时,这将意味着在服务器端引入了许多新代码.

Using projections, I will have to specify which subset of fields have to be copied, and this subset may be different in each context (as, ideally, a DTO would differ). This will mean a lot of new code introduced to the server side, when there should be the second option.

使用ValueInjecter,我将能够在一次调用中按惯例填充DTO,而无需编写特定的预测或将其保留到将来.也就是说,如果我能够让ValueInjecter忽略代理对象.

Using ValueInjecter, I will be able to populate the DTOs by convention in one call, without writing specific projections, or having to maintain those into the future. That is, if I am able to have ValueInjecter ignore Proxy objects.

鉴于在我的情况下使用投影是一个不错的方法,但不是理想的解决方案,是否可以配置类似ValueInjecter的工具来复制POCO,而无需复制代理或在复制时触发急/懒加载?

Given that using projections is a good but not ideal solution in my situation, is there a way to configure something like ValueInjecter to copy POCOs without copying proxies or triggering eager/lazy loads on copy?

推荐答案

我建议使用 SmartConventionInjection (您需要将链接页面中的代码复制到您的解决方案中)

for ValueInjecter solution I recommend using SmartConventionInjection (you need to copy the code from the linked page into your solution)

,然后指定一个不会影响代理属性的约定

and after specify a convention that won't touch the proxy properties

这是一个开始:

public class MapPoco: SmartConventionInjection
{
     protected override bool Match(SmartConventionInfo c)
     {
         return c.SourceProp.Name == c.TargetProp.Name;
     }
}

这篇关于将NHibernate POCO复制到DTO,而不会触发延迟加载或急切加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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