【问题标题】:Nhibernate bag with bindingsource带有 bindingsource 的休眠包
【发布时间】:2013-08-02 20:09:19
【问题描述】:

我正在使用绑定源从 Nhibernate 列表中填写表单:

public class Customer{
    public string Name { get; set;}
    public IList<Order> Orders { get; set;}
}

bindingSourceCustomer.DataSource = session.Query<Customer>().ToList();
bindingSourceOrder.DataSource = bindingSourceCustomer;
bindingSourceOrder.DataMember = "Orders";

现在当我打电话时

bindingSourceOrder.AddNew();

抛出异常:

值“System.Object”不是“Model.Order”类型,不能是 在这个通用集合中使用。

现在我将第一行改为:

bindingSourceCustomer.DataSource = session.Query<Customer>().Select(customer => 
    { 
      customer.Orders = customer.Orders.ToList(); 
      return customer;
    })
    .ToList();

它起作用了,原因是因为 Nhibernate 使用 PersistentBag 作为 IList 的实现,这显然不适用于绑定源(据我所知)。

有什么建议如何让 Nhibernate 返回 List 类,或者如何解决绑定源的问题?

【问题讨论】:

    标签: c# winforms nhibernate collections bindingsource


    【解决方案1】:

    那是因为 BindingSource 无法发现列表类型: NHibernate 的持久化包没有 ITypedList 接口,也没有公共的 Indexer Property。 在添加映射之前,您需要将 NHibernate CollectionTypeFactory 替换为自定义的。 我附上我的实现:

    PersistentGenericBag:

    public class EnhancedPersistentGenericBag<T> : PersistentGenericBag<T> , ITypedList
    {
            public EnhancedPersistentGenericBag(ISessionImplementor session, ICollection<T> coll) : base(session, coll) { }
    
            public EnhancedPersistentGenericBag(ISessionImplementor session) : base(session) { }
    
            public EnhancedPersistentGenericBag() { }
    
            public new T this[int index]
            {
                get
                {
                    return (T)base[index];
                }
                set
                {
                    base[index] = value;
                }
            }
    
            public string GetListName(PropertyDescriptor[] listAccessors) { return GetType().Name; }
    
            public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
            {
                return TypeDescriptor.GetProperties(typeof(T));
            }
        }
    

    CollectionTypeFactory:

    public class EnhancedCollectionTypeFactory : DefaultCollectionTypeFactory
    {
    
    
        public override CollectionType Bag<T>(string role, string propertyRef, bool embedded)
        {
    
             return  new EnhancedGenericBagType<T>(role, propertyRef);
        }
    
    }
    

    通用包类型:

    public class EnhancedGenericBagType<T> : BagType
        {
            public EnhancedGenericBagType(string role, string propertyRef) :
                base(role, propertyRef, false) { }
    
            public override IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister, object key)
            {
                return new EnhancedPersistentGenericBag<T>(session);
            }
    
            public override IPersistentCollection Wrap(ISessionImplementor session, object collection)
            {
                return new EnhancedPersistentGenericBag<T>(session, (ICollection<T>)collection);
            }
    
            public override Type ReturnedClass
            {
                get
                {
                    return typeof(ICollection<T>);
                }
            }
    
            protected override void Add(object collection, object element)
            {
                ((ICollection<T>)collection).Add((T)element);
            }
    
            protected override void Clear(object collection)
            {
                ((ICollection<T>)collection).Clear();
            }
    
            public override object Instantiate(int anticipatedSize)
            {
                if (anticipatedSize > 0)
                    return new List<T>(anticipatedSize + 1);
                else
                    return new List<T>();
            }
        }
    

    如何覆盖默认的 CollectionTypeFactory:

    Configuration cfg = new Configuration();
    cfg.CollectionTypeFactory<EnhancedCollectionTypeFactory>();
    

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 2017-08-14
      • 2015-03-13
      • 1970-01-01
      • 2016-06-27
      • 2016-01-12
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多