有人可以更好地解释 nHibernate 中的“投影"是什么? [英] Can someone better explain what 'Projections' are in nHibernate?

查看:21
本文介绍了有人可以更好地解释 nHibernate 中的“投影"是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为 nHibernate 及其实用程序库 fluent nhibernate 的新用户,我正在努力学习足够多的知识,以应对使用良好数据库的危险.

As a new user of nHibernate and its utility library, fluent nhibernate, I am trying to learn enough to be dangerous with a good database.

我在理解投影的概念时遇到了极大的困难.具体来说,它们到底是什么?

I am having an exceptionally great deal of difficulty understanding the concept of Projections. Specifically, What in the world are they?

我确实对什么是投影?"和nHibernate 中的项目"和nHibernate、投影、定义进行了精确搜索',等等.我仍然很困惑.迄今为止最有用的帖子是 This other StackOverflow QuestionColin Ramsay 的这篇博文.但我仍然非常困惑.我对数据库的了解充其量只是入门级.

I have literally done exact searches on 'What are projections?' and 'Projects in nHibernate' and 'nHibernate, Projections, Definition', etc. And I am still very confused. The most helpful posts so far are This other StackOverflow Question and This Blog Post by Colin Ramsay. But I am still vastly confused. My knowledge of databases is still entry-level at best.

我真的不明白什么是投影,我为什么要使用它们,它们正在完成什么,等等.我在博客文章中看到他正在使用它们来获取整数列表(我假设是主键)这样他就可以在不同的查询中使用它们,但这在它的运作方式和原因方面有点模糊.

I do not really understand what projections are, why I would want to use them, what they are accomplishing, etc. I see in the blog post that he is using them to get a list of integers (I presume Primary Keys) so that he can use them in a different query, but this is kind of nebulous in the way it is functioning and the why.

推荐答案

这是一个实际的例子.

假设您有一家在线商店,并且您的域类之一是Samsung"等品牌.这个类有一大堆与之相关的属性,可能是一个整数 Identity、一个 Name、一个自由文本 Description 字段、一个对Vendor 对象,等等.

Let's say that you have an online store and one of your domain classes is a Brand like "Samsung". This class has a boatload of properties associated with it, perhaps an integer Identity, a Name, a free-text Description field, a reference to a Vendor object, and so on.

现在假设您想要显示一个菜单,其中列出了您的在线商店提供的所有品牌.如果您只执行 session.CreateCriteria().List(),那么您确实会获得所有品牌.但是您也会从数据库中提取所有长的 Description 字段和对 Vendor 的引用,并且您不需要它来显示菜单;您只需要 NameIdentity.在性能方面,从数据库中提取所有这些额外数据会减慢速度并且是不必要的.

Now let's say that you want to display a menu with a list of all the brands offered on your online store. If you just do session.CreateCriteria<Brand>().List(), then you are indeed going to get all of the brands. But you'll also have sucked all of the long Description fields and references to Vendors from the database, and you don't need that to display a menu; you just need the Name and the Identity. Performance-wise, sucking all of this extra data down from the database slows things down and is unnecessary.

相反,您可以创建一个投影"对象,其中仅包含 Identity 和调用它的 Name,例如,NameIdentityPair:

Instead, you can create a "projection" object that contains just the Identity and the Name calling it, say, NameIdentityPair:

public class NameIdentityPair
{
    public int Identity { get; set; }
    public string Name { get; set; }
}

你可以告诉 NHibernate 只选择你真正需要执行手头任务的数据,告诉它把结果集转换到你的投影上:

And you could tell NHibernate to only select the data that you really need to perform the task at hand by telling it to transform the result set onto your projection:

var brandProjections = this.session.CreateCriteria<Brand>()
    .SetProjection(Projections.ProjectionList()
        .Add(Projections.Property("Name"), "Name")
        .Add(Projections.Property("Identity"), "Identity"))
    .SetResultTransformer(Transformers.AliasToBean<NameIdentityPair>())
    .List<NameIdentityPair>();

foreach (var brandProjection in brandProjections)
{
    Console.WriteLine(
        "Identity: {0}, Name: {1}", 
        brandProjection.Identity, 
        brandProjection.Name);
}

现在你没有一个 Brand 的列表,而是一个 NameIdentityPair 的列表,NHibernate 只会发出一个类似于 SELECT 的 SQL 语句b.Identity, b.Name 来自 dbo.Brand b 来获得这个投影,而不是一个庞大的 SQL 语句,它抓取所有必要的东西来水合 Brand 对象(例如,SELECT b.Identity, b.Name, b.Description from dbo.brand b left join dbo.vendor v ....).

Now you don't have a list of Brands but instead a list of NameIdentityPairs, and NHibernate will have only issued a SQL statement like SELECT b.Identity, b.Name from dbo.Brand b to obtain this projection, as opposed to a massive SQL statement that grabs everything necessary to hydrate a Brand object (e.g., SELECT b.Identity, b.Name, b.Description from dbo.brand b left join dbo.vendor v ....).

希望这会有所帮助.

这篇关于有人可以更好地解释 nHibernate 中的“投影"是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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