NHibernate的手动控制取

NHibernate的手动控制取

本文介绍了NHibernate的手动控制取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NHibernate的,我想手动控制获取相关的实体。

I am using NHibernate and I want to control fetching related entities manually.

下面是我的示例实体

public class Post
{
    public virtual long Id { get; set; }
    public virtual string Title { get; set; }
    public virtual User User { get; set; }
    public virtual IList<Like> Likes { get; set; }
    public virtual IList<Tag> Tags { get; set; }
}



我希望behvaiour如下:

The behvaiour I expect is as follows:

session.Query<Post>().ToList();

在这种查询我想发表实体有:

After this kind of query I want Post entities to have:


  1. 原始属性设置

  2. 用户属性不为null,但只有Id属性集。

  3. 喜欢标签为null或空集

  1. Primitive properties are set
  2. User property is not null but only have Id property set.
  3. Likes and Tags are null or empty collection

-

session.Query<Post>()
    .Fetch(p => p.User)
    .Fetch(p => p.Tags)
    .ToList();

和这种查询我想以后发表实体有:

And after this kind of query I want Post entities to have:


  1. 原始属性设置

  2. 用户属性不为null和属性进行设置。

  3. 标签不为空,所有项目已全部属性设置

  4. 喜欢为空或空集

  1. Primitive properties are set
  2. User property is not null and properties are set.
  3. Tags is not null and all items have all properties set
  4. Likes is null or empty collection

基本上我从NHibernate的希望是什么,而不是除非我要求它取回并不会导致NHibernate的特定异常(惰性初始模式等)获取任何相关的实体,当我尝试访问未读取性能。我期望的行为是不懒,也不急于。

Basically what I want from NHibernate is, not to fetch any related entities unless I ask for it to fetch and not cause an NHibernate specific exception (LazyInitialization etc.) when I try to access not fetched properties. The behaviour I expect is not lazy nor eager.

在你尝试过什么的评论,我试着用 LazyLoad几乎所有组合()取等。

Before "what have you tried" comments, I tried almost all combinations with LazyLoad(), Not, Fetch etc. in Fluent NHibernate mapping configuration along with both statless and stateful sessions.

推荐答案

我无法弄清楚如何处理未初始化的实例,而不调用validate前参考:

I can't figure out how to deal with uninitialized instances without validate before call a reference:

NHibernateUtil.IsInitialized(entityOrCollection)

和/或

NHibernateUtil.IsPropertyInitialized(obj, "propertyName")

这篇关于NHibernate的手动控制取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:07