vij*_*ter 1 c# nhibernate asp.net-mvc orm
我在C Sharp中创建了一个不区分大小写的字典,如下所示。由于字典不区分大小写,因此以下代码将引发异常。
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
}
Run Code Online (Sandbox Code Playgroud)
但是在使用NHibernate中的某些聚合类映射此字典(名称)时,我无法获得此异常。即,可以将具有不同大小写的相同文本添加到按键。
进行名称字典处理时,会被NHibernate动态创建的字典(使用反射)覆盖,区分大小写。
有人可以建议,如何将不区分大小写的字典映射到NHibernate。
谢谢,维杰
已经过了5年,但是由于没有确切的答案,也许有人会看到这一点。
杰米(Jamie)关于使用CustomCollection的答案是完全正确的,这是一个非常简单的类,映射和IUserCollectionType的示例,可以帮助您顺利进行。
除了Instantiate之外,大多数这些方法都是从实际的nhibernate代码中窃取的。
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
927 次 |
| 最近记录: |