NHibernate 中的 List vs Set vs Bag [英] List vs Set vs Bag in NHibernate
问题描述
NHibernate 映射文件中的 list、set 和 bag 有什么区别?每个与 .NET 集合有何关系?
What's the difference between a list, set and bag in the NHibernate mapping file? How does each relate to the .NET collections?
推荐答案
NHibernate 语义:
NHibernate semantics:
List:实体的有序集合,允许重复.在代码中使用 .NET
IList
.索引列需要在 NHibernate 中进行映射.
List: Ordered collection of entities, duplicate allowed. Use a .NET
IList
in code. The index column will need to be mapped in NHibernate.
Set:唯一实体的无序集合,不允许重复.在代码中使用 Iesi.Collection.ISet
(NH v4 之前)或 System.Collections.Generic.ISet
(NH v4+).重要的是要覆盖GetHashCode
和Equals
以指示重复的业务定义.可以通过定义 orderby 或通过定义导致 SortedSet
结果的比较器进行排序.
Set: Unordered collection of unique entities, duplicates not allowed. Use Iesi.Collection.ISet
in code (NH prior to v4) or System.Collections.Generic.ISet
(NH v4+). It is important to override GetHashCode
and Equals
to indicate the business definition of duplicate. Can be sorted by defining an orderby or by defining a comparer resulting in a SortedSet
result.
Bag:实体的无序列表,允许重复.在代码中使用 .NET ICollection
.该列表的索引列未映射,NHibernate 不支持该列.
Bag: Unordered list of entities, duplicates allowed. Use a .NET ICollection<T>
in code. The index column of the list is not mapped and not honored by NHibernate.
这篇关于NHibernate 中的 List vs Set vs Bag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!