有时候NHibernate不会返回代理对象吗? [英] Are there occasions when NHibernate won't return a proxy object?

查看:74
本文介绍了有时候NHibernate不会返回代理对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Fluent NHibernate(并且扩展为NHibernate).我正在使用带有一些替代的自动映射,但是我认为这对问题不重要(当然,如果我在这里错了,我会很乐意更新问题).

I'm learning Fluent NHibernate (and by extension, NHibernate generally). I'm using auto-mapping with some overrides, but I don't think that is important for the question (of course, if I'm wrong here I'll gladly update the question).

给出一个ISession(以及更多假定的变量),我可以通过其ID返回实体:

Given an ISession (and a few more assumed variables) I can return entities by their ID:

using (var session = SessionFactory.OpenSession())
{
    var user = session.Get<User>(userId);
}

我有限的理解是NHibernate围绕User映射实体创建了一个代理,但是当我对其进行测试时(

My limited understanding was that NHibernate creates a proxy around the User mapped entity, yet when I test it (based on this):

Assert.That(user is INHibernateProxy, "Not a proxy.");

似乎我的实例不是代理.

It appears as though my instance is not a proxy.

有没有不使用代理的情况?我希望获得缺少的东西",并祈祷这不是星期五下午的脑筋急转弯.

Are there occasions where proxies are not used? I'm hoping for the "missing piece" and praying this isn't the Friday afternoon brain-fail.

推荐答案

通常,代理用于延迟加载.每当您通过Get等获取权限时,您不会得到代理,而是真正的对象. NHibernate在不需要时不使用代理.

Generally proxies are for lazy loading. Whenever you're fetching entitles by Get etc., you're not getting proxies, but the real objects. NHibernate don't use proxies when not necessary.

但是,如果user具有Address,则user.Address is INHibernateProxy将为true(除非为此关系关闭了延迟加载).

But if user has an Address, user.Address is INHibernateProxy will be true (unless lazy loading is turned off for this relationship).

有关NHibernate如何以及何时处理代理的详细信息,请参见

For more info about how and when NHibernate handles proxies, see this article.

这篇关于有时候NHibernate不会返回代理对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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