代码之家  ›  专栏  ›  技术社区  ›  vijaysylvester

如何将不区分大小写的字典映射到NHibernate。?

  •  1
  • vijaysylvester  · 技术社区  · 16 年前

    我创建了一个 不区分大小写

    IDictionary<string, ABCentre> names = new Dictionary<string, ABCentre>(StringComparer.OrdinalIgnoreCase);
            names.Add("LC001", new ABCentre());
    
            if (names.ContainsKey("lc001"))
            {
                names.Add("lc001", new ABCentre());// Exception , same key exists  
            }
    

    但是,当用NHibernate中的某个聚合类映射这个字典(名称)时,我无法得到这个异常。i、 例如,可以在键中添加具有不同大小写的相同文本。

    使用NHibernate动态创建的名称字典(使用反射)覆盖名称字典,该字典区分大小写。

    有人能建议如何将不区分大小写的字典映射到NHibernate吗。?

    维杰

    2 回复  |  直到 16 年前
        1
  •  2
  •   Jamie Ide    16 年前

    custom collection NHibernate.Collection.PersistentGenericMap<TKey, TValue> ).

        2
  •  5
  •   Suhaib Janjua    11 年前

    Jamie关于使用CustomCollection的回答是完全正确的,这是一个非常简单的类、映射和IUserCollectionType示例,应该可以帮助您继续使用。

    nhibernate code 除了实例化。

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