Nhibernate - 对象的部分加载
我有一个代表网站顶部菜单的 MenuObject 类。
除其他属性外,该对象还具有以下属性:
public class MenuObject
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual List<MenuObject> Children { get; set; }
}
映射已配置为使用 fluid nhibernate 中的 Not.LazyLoad() 定义急切地加载 Children 对象。
我现在遇到一个问题,我想使用这些对象来填充面包屑控件和侧菜单控件 - 对于这些控件,我需要的是:
- ID 和标题属性
- Children 集合(也只包含上述两个属性)
我不不想加载主对象的所有属性以及每个子对象的所有属性,因为这太过分了。
我已经设法使用 Nhibernate Linq 仅返回主对象的属性,但是如何修改查询以对子对象执行相同的操作? (参见???)
return (from mnu in session.Query<MenuObject>()
(select new MenuObject()
{
Id = mnu.Id
Title = mnu.Title,
Children = ???
}
----------------编辑---------------------
我已经得到了这个工作,但我缺少一个 where 子句 - 这是将“子项”设置为所有 MenuObject 实例的列表,无论关系如何 - 显然我只希望它向每个父对象添加合法的子对象 - 任何人都可以帮忙吗?谢谢
from menuobject in session
.Query<MenuObject>()
where menuobject.Level == level
select new MenuObject()
{
Title = menuobject.Title,
Url = menuobject.Url,
Children = session.CreateCriteria<MenuObject>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Title"), "Title")
.Add(Projections.Property("Url"), "Url"))
.SetResultTransformer(Transformers.AliasToBean(typeof (MenuObject)))
.List<MenuObject>()
}
.ToList();
I have a MenuObject class that represents a websites Top Menu.
This object has amongst other properties, the following:
public class MenuObject
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
public virtual List<MenuObject> Children { get; set; }
}
The mapping has been configured to eagerly load the Children objects using the Not.LazyLoad() definition in fluent nhibernate.
I now have a problem where I want to use these objects to populate a breadcrumb control and a sidemenu control - For these controls, All I need is:
- The ID and Title Properties
- The Children collection (also only containing the above two properties)
I don't want to load ALL the properties of my main object as well as ALL the properties of eachchild object as it's just overkill.
I've managed to use Nhibernate Linq to retrurn ONLY properties of the main object, but how do I amend the query to do the same for child objects? (see ???)
return (from mnu in session.Query<MenuObject>()
(select new MenuObject()
{
Id = mnu.Id
Title = mnu.Title,
Children = ???
}
----------------EDIT---------------------
I've got this to work but I'm missing a where clause - this is setting the 'Children' to a list of all MenuObject instances regardless of relationship - obviously I only want it to add legitimate child objects to each parent object - can anyone assist? Thanks
from menuobject in session
.Query<MenuObject>()
where menuobject.Level == level
select new MenuObject()
{
Title = menuobject.Title,
Url = menuobject.Url,
Children = session.CreateCriteria<MenuObject>()
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("Title"), "Title")
.Add(Projections.Property("Url"), "Url"))
.SetResultTransformer(Transformers.AliasToBean(typeof (MenuObject)))
.List<MenuObject>()
}
.ToList();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,我错过了多个孩子的部分。另外,我更喜欢 ViewModel 或匿名类型,而不是部分加载的 Domainobjects,这可能会导致混乱。
sorry i missed the multiple childs part. Also i would favor ViewModels or anonymous types instead of partial loaded Domainobjects which could cause confusion down the road.