这是5年后,但也许有人会看到这一点,因为没有可靠的答案。
杰米关于使用CustomCollection的回答是完全正确的,这是一个非常简单的类,映射和IUserCollectionType的例子,应该让你去。
这些方法中的大多数都是从实际nhibernate code中被盗取的,但Instantiate除外。
public class meta {
public virtual Guid id { get; set; }
public virtual IDictionary<string, string> data { get; set; }
public meta() {
data = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
}
}
public class metamap : ClassMap<meta> {
public metamap() {
Table("metatable");
Id(x=>x.id);
HasMany(x => x.data)
.Table("metadata")
.AsMap<string>("skey")
.Element("value")
.CollectionType<DictionaryInsensitive<string>>()
.Cascade.All()
.KeyColumn("metaid");
}
}
public class DictionaryInsensitive<T> : IUserCollectionType {
bool IUserCollectionType.Contains(object collection, object entity) {
return ((IDictionary<string,T>) collection).Values.Contains((T)entity);
}
System.Collections.IEnumerable IUserCollectionType.GetElements(object collection) {
return ((IDictionary<string,T>) collection).Values;
}
object IUserCollectionType.IndexOf(object collection, object entity) {
var dictionary = (IDictionary<string, T>)collection;
return dictionary
.Where(pair => Equals(pair.Value, entity))
.Select(pair => pair.Key)
.FirstOrDefault();
}
object IUserCollectionType.Instantiate(int anticipatedSize) {
return
new Dictionary<string, T>(
StringComparer.InvariantCultureIgnoreCase);
}
NHibernate.Collection.IPersistentCollection IUserCollectionType.Instantiate(NHibernate.Engine.ISessionImplementor session, NHibernate.Persister.Collection.ICollectionPersister persister) {
return new PersistentGenericMap<string, T>(session);
}
object IUserCollectionType.ReplaceElements(object original, object target, NHibernate.Persister.Collection.ICollectionPersister cp, object owner, System.Collections.IDictionary copyCache, NHibernate.Engine.ISessionImplementor session) {
IDictionary<string, T> result = (IDictionary<string, T>)target;
result.Clear();
IEnumerable<KeyValuePair<string, T>> iter = (IDictionary<string, T>)original;
foreach (KeyValuePair<string, T> me in iter) {
string key = (string)cp.IndexType.Replace(me.Key, null, session, owner, copyCache);
T value = (T)cp.ElementType.Replace(me.Value, null, session, owner, copyCache);
result[key] = value;
}
var originalPc = original as IPersistentCollection;
var resultPc = result as IPersistentCollection;
if (originalPc != null && resultPc != null) {
if (!originalPc.IsDirty)
resultPc.ClearDirty();
}
return result;
}
NHibernate.Collection.IPersistentCollection IUserCollectionType.Wrap(NHibernate.Engine.ISessionImplementor session, object collection) {
var dict = new Dictionary<string, T>();
return new PersistentGenericMap<string, T>(session, (IDictionary<string,T>)collection);
}
}
谢谢杰米,你可以发布一个示例代码。 – vijaysylvester 2010-03-12 04:56:20
对不起,但链接的样本是我能做的最好的。我不使用自定义集合类型。 – 2010-03-12 13:30:43