有人能更好地解释什么是“预测”是NHibernate的? [英] Can someone better explain what 'Projections' are in nHibernate?

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

问题描述

例如NHibernate的新用户和它的实用程序库,功能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的,预测,定义的'等。而我还是很困惑。最有用的帖子至今都这个计算器等问题并的科林·拉姆齐这个博客帖子。但我仍然大大困惑。我的数据库中的知识仍然是入门级的最好的。

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.

推荐答案

下面是一个实际的例子。

Here's a practical example.

让我们说你有一个在线商店,你的域类中的一个是品牌如三星。这个类有它,也许一个整数身份,一个名称,自由文本关联的属性的一大堆说明字段,一个参照供应商对象,依此类推。

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<品牌方式>()名单(),那么你确实会得到所有的品牌。但你也有吸众长说明字段和引用供应商■从数据库,你不需要那么显示菜单;你只需要在名称身份。性能方面,从数据库中吸吮所有这些额外的数据的下降会减慢速度,是没有必要的。

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.

相反,你可以创建一个只包含一个投影对象身份名称调用它,比方说, NameIdentityPair <​​/ code>:

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);
}

现在你不必品牌列表取值而是 NameIdentityPair <​​/ code> S,和NHibernate将有一个列表只发出像 SELECT b.Identity一个SQL语句, b.Name从dbo.Brand b 来获得该投影,而不是,抓住必要水合物品牌对象的一切(大规模的SQL语句例如, SELECT b.Identity,b.Name,b.Description从dbo.brand b左加入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天全站免登陆