在nHibernate关系中使用Lite版本的实体? [英] Using Lite Version of Entity in nHibernate Relations?

查看:137
本文介绍了在nHibernate关系中使用Lite版本的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下创建一个实体的更轻版本是一个好主意,只是出于性能原因指向同一个表,但映射的列较少。例如,如果我有一个有50列的联系表,并且在少数相关实体中,我可能对FirstName和LastName属性感兴趣,那么创建Contact表的轻量版本是个不错的主意。例如,

  public class ContactLite 
{
public int Id {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}

}

也可以将多个类映射到同样的表?

解决方案

这不是一个好主意。相反,总是映射完整的类并创建可以使用 Transformers.AliasToBean 或LINQ进行投影的小类。



后一个例子:

  var lightContacts =(来自session.Linq中的联系人< Contact>()
where contact.Country =Argentina
选择新的LightContact
{
Id = contact.Id
FirstName = contact.FirstName,
LastName = contact.LastName
})
.ToList();

这将只从数据库中选择这三个字段,即使是用不同的字段过滤也是如此。 p>

值得注意的是,对于LINQ,您还可以使用匿名类型来选择您想要的任何投影,而无需创建其他类型或映射。


Is it a good idea to create a lighter version of an Entity in some cases just for performance reason pointing to same table but with fewer columns mapped. E.g If I have a Contact Table which has 50 Columns and in few of the related entities I might be interested in FirstName and LastName property is it a good idea to create a lightweight version of Contact table. E.g.

public class ContactLite
{
   public int Id {get; set;}
   public string FirstName {get; set;}
   public string LastName {get; set;}

}

Also is it possible to map multiple classes to same table?

解决方案

It's not a good idea. Instead, always map the full class and create smaller ones that you can project on using Transformers.AliasToBean or LINQ.

An example of the latter:

var lightContacts = (from contact in session.Linq<Contact>()
                     where contact.Country = "Argentina"
                     select new LightContact
                            {
                                Id = contact.Id
                                FirstName = contact.FirstName,
                                LastName = contact.LastName
                            })
                    .ToList();

This will only select those three fields from the DB, even when filtering by a different one.

It's worth noting that, with LINQ, you could also use an anonymous type to select any projection you want without creating additional types or mappings.

这篇关于在nHibernate关系中使用Lite版本的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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