NHibernate proxy causing problems with databinding

后端 未结 5 1322
庸人自扰
庸人自扰 2021-01-20 05:34

I have a gridview that is bound to the result from an nhibernate query. If the first item in the list is edited the following exception is thrown:

System.Refle

5条回答
  •  生来不讨喜
    2021-01-20 06:06

    Very late, but should help others with the same problem. The solution I used is to wrap a custom list (in this case a NotificationList) around the field in the getter.

    private IList _parameters = new List();  
    get  
    {  
        return new NotificationList(_parameters);  
    }
    

    This list is a wrapper around the list, so that databinding will be forwarded to the original list.

    public class NotificationList : IList, IList    
    {
        IList myList;
        public NotificationList(IList list)
        {
            myList = list;
        }
        int IList.Add(object item)
        {
            myList.Add ((T) item);
        } 
        // implement both IList and IList
        // ...
    }
    

    For me this fixed the issue with the databinding, but created a side effect where every time the session was flushed all the items in the collection get updated in the DB, wether they changed or not. To resolve that, I changed the mapping to access the field directly. See this on Hibernate, which applies to NHibernate as well.
    This is the new (Fluent) mapping:

    HasMany(x => x.Parameters)
           .Cascade.All()
           .Access.CamelCaseField(Prefix.Underscore);
    

提交回复
热议问题