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

查看:33
本文介绍了将 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?

推荐答案

对于 ValueInjecter 解决方案,我建议使用 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天全站免登陆