开发者

How to map a case insensitive dictionary to NHibernate.?

I have created a case insensitive dictionary in C Sharp as follows . And the follow开发者_如何转开发ing code throws an exception, since the dictionary is case insensitive.

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  
        }

But while mapping this dictionary(names) with some aggregation class in NHibernate, i am not able to get this exception. i.e., same text with different cases can be added to the keys .

Does the names dictionary, gets overridden with the one that NHibernate creates dynamically (using reflection), which is case sensitive.

Can some one suggest , how to map a case insensitive dictionary to NHibernate.?

thanks , vijay


It's 5 years later, but maybe someone will see this since there was no solid answer.

Jamie's answer about using a CustomCollection is completely correct, this is a very simple example of a class, a mapping and an IUserCollectionType that should get you going.

Most of these methods were stolen from the actual nhibernate code with the exception of 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);
    }
}


You'll have to create and map a custom collection to enable NHIbernate to use your collection type. NHibernate has its own implementation of IDictionary that it uses for collections mapped as map (NHibernate.Collection.PersistentGenericMap<TKey, TValue>).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜