-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: IUserCollectionType, BindingList, Generic
PostPosted: Thu Mar 29, 2007 11:01 am 
Newbie

Joined: Thu Mar 29, 2007 9:39 am
Posts: 10
Hibernate version: 1.2.0

Hi,
I'm trying to get a bindinglist as result of a one-to-many relation.
When a set my property type to bindinglist(Of T), i get this error :

Quote:
NHibernate.MappingException was unhandled
Message="Invalid mapping information specified for type MonNameSpace.clsEntreprise, check your mapping file for property type mismatches"
Source="NHibernate"
StackTrace:
at NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values)
at NHibernate.Impl.SessionImpl.InitializeEntity(Object obj)
at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
at NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, Type optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId)
at NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session)
at NHibernate.Impl.SessionImpl.DoLoad(Type theClass, Object id, Object optionalObject, LockMode lockMode, Boolean checkDeleted)
at NHibernate.Impl.SessionImpl.DoLoadByClass(Type clazz, Object id, Boolean checkDeleted, Boolean allowProxyCreation)
at NHibernate.Impl.SessionImpl.Load(Type clazz, Object id)
at MonNameSpace.facEntreprise.SelectOne(Int32 EntrepriseID) in C:\Documents and Settings\cdl\Desktop\test2\test2\test2\factory\facEntreprise.vb:line 19
at MonNameSpace.Form1.Form1_Load(Object sender, EventArgs e) in C:\Documents and Settings\cdl\Desktop\test2\test2\test2\Form1.vb:line 11
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at MonNameSpace.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


With this inner error :

Quote:
{"Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag`1[MonNameSpace.clsAdresse]' to type 'System.ComponentModel.BindingList`1[MonNameSpace.clsAdresse]'."}


So I decided to create a custom Collection which Inherits BindingList(Of T), and i found some example, to Implements IUserCollectionType.
Here the code :

Mapping :
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="MonNameSpace.clsEntreprise, MonAssembly" table="tblEntreprises">

      <id name="EntrepriseID" column="EntrepriseID" type="Int32">
         <generator class="identity" />
      </id>
      <property name="Nom" column="Nom" type="String" length="50" />
      <property name="CompteBancaire" column="CompteBancaire" type="String" length="30" />
      <property name="Iban" column="Iban" type="String" length="30" />
      <property name="Swift" column="Swift" type="String" length="30" />
      <property name="NoTva" column="NoTva" type="String" length="20" />
      <property name="NoSun" column="NoSun" type="String" length="20" />
      <property name="Tel" column="Tel" type="String" length="20" />
      <property name="Fax" column="Fax" type="String" length="20" />
      <property name="Email" column="Email" type="String" length="50" />
      <property name="Site" column="Site" type="String" length="50" />
      <property name="Langue" column="Langue" type="String" length="10" />
      <property name="Info" column="Info" type="String" length="500" />
      <property name="Supprime" column="Supprime" type="Boolean" />
      <property name="ChangeDate" column="ChangeDate" type="DateTime" />
      <property name="ChangeUser" column="ChangeUser" type="String" length="20" />
      <property name="Flag" column="Flag" type="Int32" length="1" />

      <bag name="Adresses" cascade="save-update" collection-type="MonNameSpace.MaListe`1[[MonNameSpace.clsAdresse, MonAssembly]],MonAssembly">
         <key column="EntrepriseID"/>
         <one-to-many class="MonNameSpace.clsAdresse, MonAssembly"/>
      </bag>
   </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">

   <class name="MonNameSpace.clsAdresse, MonAssembly" table="tblAdresses" dynamic-update="true">

      <id name="AdresseID" column="AdresseID" type="Int32">
         <generator class="identity" />
      </id>
      <property name="EntrepriseID" column="EntrepriseID" type="Int32" length="4" />
      <property name="Rue" column="Rue" type="String" length="50" />

   </class>

</hibernate-mapping>


Data Access :
Code:
Public Class clsEntreprise

    Protected m_EntrepriseID As Integer
    Protected m_Nom As String
    Protected m_CompteBancaire As String
    Protected m_Iban As String
    Protected m_Swift As String
    Protected m_NoTva As String
    Protected m_NoSun As String
    Protected m_Tel As String
    Protected m_Fax As String
    Protected m_Email As String
    Protected m_Site As String
    Protected m_Langue As String
    Protected m_Info As String
    Protected m_Supprime As Boolean
    Protected m_ChangeDate As Date
    Protected m_ChangeUser As String
    Protected m_Flag As Integer
    Private m_IListAdresses As IMaListe(Of clsAdresse) = New MaListe(Of clsAdresse)()


    Public Overridable Property EntrepriseID() As Integer
        Get
            Return m_EntrepriseID
        End Get
        Set(ByVal value As Integer)
            m_EntrepriseID = value
        End Set
    End Property

    Public Overridable Property Nom() As String
        Get
            Return m_Nom
        End Get
        Set(ByVal value As String)
            m_Nom = value
        End Set
    End Property

...
...

    Public Overridable Property Adresses() As IMaListe(Of clsAdresse)
        Get
            Return m_IListAdresses
        End Get
        Set(ByVal Value As IMaListe(Of clsAdresse))
            m_IListAdresses = Value
        End Set
    End Property

End Class


Code:
Public Class clsAdresse

    Protected m_AdresseID As Integer
    Protected m_EntrepriseID As Integer
    Protected m_Rue As String


    Public Overridable Property AdresseID() As Integer
        Get
            Return m_AdresseID
        End Get
        Set(ByVal value As Integer)
            m_AdresseID = value
        End Set
    End Property

    Public Overridable Property EntrepriseID() As Integer
        Get
            Return m_EntrepriseID
        End Get
        Set(ByVal value As Integer)
            m_EntrepriseID = value
        End Set
    End Property

    Public Overridable Property Rue() As String
        Get
            Return m_Rue
        End Get
        Set(ByVal value As String)
            m_Rue = value
        End Set
    End Property

End Class


UserCollection :
Code:
Imports NHibernate.Engine
Imports NHibernate.UserTypes
Imports NHibernate.Collection
Imports NHibernate.Collection.Generic
Imports NHibernate.Persister.Collection

Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Specialized


Public Class MaListe(Of T)
    Inherits BindingList(Of T)
    Implements IMaListe(Of T), IUserCollectionType


#Region "IUserCollectionType Implementation"

    Public Function Instantiate(ByVal session As ISessionImplementor, ByVal persister As ICollectionPersister) As IPersistentCollection Implements IUserCollectionType.Instantiate

        Return New PersistentGenericMaListeBag(Of T)(session)

    End Function

    Public Function Wrap(ByVal session As ISessionImplementor, ByVal collection As Object) As IPersistentCollection Implements IUserCollectionType.Wrap

        Return New PersistentGenericMaListeBag(Of T)(session, CType(collection, MaListe(Of T)))

    End Function

    Public Function GetElements(ByVal collection As Object) As IEnumerable Implements IUserCollectionType.GetElements

        Return CType(collection, IEnumerable)

    End Function

    Public Function Contains(ByVal collection As Object, ByVal entity As Object) As Boolean Implements IUserCollectionType.Contains

        Return (CType(collection, IMaListe(Of T))).Contains(CType(entity, T))

    End Function

    Public Function IndexOf(ByVal collection As Object, ByVal entity As Object) As Object Implements IUserCollectionType.IndexOf

        Return (CType(collection, IList(Of T))).IndexOf(CType(entity, T))

    End Function

    Public Function ReplaceElements(ByVal original As Object, ByVal target As Object, ByVal persister As ICollectionPersister, ByVal owner As Object, ByVal copyCache As System.Collections.IDictionary, ByVal session As ISessionImplementor) As Object Implements IUserCollectionType.ReplaceElements

        Dim Result As IMaListe(Of T) = CType(target, IMaListe(Of T))

        Result.Clear()

        For Each item As Object In (CType(original, IEnumerable))

            Result.Add(CType(item, T))

        Next

        Return Result

    End Function

    Public Function Instantiate() As Object Implements IUserCollectionType.Instantiate

        Return New MaListe(Of T)()

    End Function

#End Region


IUserCollection :
Code:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Collections
Imports System.Collections.Specialized

Public Interface IMaListe(Of T)
    Inherits IBindingList

End Interface


Persistent Wrapper :
Code:
Imports NHibernate.Engine
Imports NHibernate.Collection.Generic

Public Class PersistentGenericMaListeBag(Of T)
    Inherits PersistentGenericBag(Of T)
    Implements IMaListe(Of T)

    Public Sub New(ByVal session As ISessionImplementor, ByVal list As MaListe(Of T))

        MyBase.New(session, list)

    End Sub

    Public Sub New(ByVal session As ISessionImplementor)

        MyBase.New(session)

    End Sub

#Region "IBindingList Implementation"

    Public Sub AddIndex(ByVal [property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.AddIndex

    End Sub

    Public Function AddNew() As Object Implements System.ComponentModel.IBindingList.AddNew
        Return False
    End Function

    Public ReadOnly Property AllowEdit() As Boolean Implements System.ComponentModel.IBindingList.AllowEdit
        Get
            Return False
        End Get
    End Property

    Public ReadOnly Property AllowNew() As Boolean Implements System.ComponentModel.IBindingList.AllowNew
        Get
            Return False
        End Get
    End Property

    Public ReadOnly Property AllowRemove() As Boolean Implements System.ComponentModel.IBindingList.AllowRemove
        Get
            Return False
        End Get
    End Property

    Public Sub ApplySort(ByVal [property] As System.ComponentModel.PropertyDescriptor, ByVal direction As System.ComponentModel.ListSortDirection) Implements System.ComponentModel.IBindingList.ApplySort

    End Sub

    Public Function Find(ByVal [property] As System.ComponentModel.PropertyDescriptor, ByVal key As Object) As Integer Implements System.ComponentModel.IBindingList.Find

    End Function

    Public ReadOnly Property IsSorted() As Boolean Implements System.ComponentModel.IBindingList.IsSorted
        Get
            Return False
        End Get
    End Property

    Public Event ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Implements System.ComponentModel.IBindingList.ListChanged

    Public Sub RemoveIndex(ByVal [property] As System.ComponentModel.PropertyDescriptor) Implements System.ComponentModel.IBindingList.RemoveIndex

    End Sub

    Public Sub RemoveSort() Implements System.ComponentModel.IBindingList.RemoveSort

    End Sub

    Public ReadOnly Property SortDirection() As System.ComponentModel.ListSortDirection Implements System.ComponentModel.IBindingList.SortDirection
        Get
            Return System.ComponentModel.ListSortDirection.Descending

        End Get
    End Property

    Private m_SortProperty As System.ComponentModel.PropertyDescriptor

    Public ReadOnly Property SortProperty() As System.ComponentModel.PropertyDescriptor Implements System.ComponentModel.IBindingList.SortProperty
        Get
            Return m_SortProperty
        End Get
    End Property

    Public ReadOnly Property SupportsChangeNotification() As Boolean Implements System.ComponentModel.IBindingList.SupportsChangeNotification
        Get
            Return False
        End Get
    End Property

    Public ReadOnly Property SupportsSearching() As Boolean Implements System.ComponentModel.IBindingList.SupportsSearching
        Get
            Return False
        End Get
    End Property

    Public ReadOnly Property SupportsSorting() As Boolean Implements System.ComponentModel.IBindingList.SupportsSorting
        Get
            Return False
        End Get
    End Property

#End Region


Factory :
Code:
Imports NHibernate
Imports NHibernate.Cfg


Public Class facEntreprise

    Public Shared Function SelectOne(ByVal EntrepriseID As Integer) As clsEntreprise

        Dim cfg As New Configuration()
        cfg.AddAssembly("MonAssembly")

        Dim factory As ISessionFactory = cfg.BuildSessionFactory()
        Dim session As ISession = factory.OpenSession()

        Return session.Load(GetType(clsEntreprise), EntrepriseID)

        session.Close()

    End Function

End Class


-----------------------------------------------------------------------------

So, i can use this :

Code:
facEntreprise.SelectOne(117).Adresses


Which return a IMaList(Of MonNameSpace.clsAdresse)

Now, I want add some custom function to my collection MaList(Of T).
When i do that, I don't see them the IMaList return.

So i add them to the Interface IMaList, and then, i must Implement them in the persistent wrapper PersistentGenericMaListeBag(Of T).

Finally, the used code is the one in PersistentGenericMaListeBag(Of T), not the one in MaList(Of T)...

So i must write my code 2 times ?
There isn't a easier/simpler way to use Bindinglist and/or custom collection ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 30, 2007 8:13 am 
Newbie

Joined: Thu Mar 29, 2007 9:39 am
Posts: 10
Nobody use custom collection and binding list ??


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 11:12 am 
Newbie

Joined: Thu Mar 29, 2007 9:39 am
Posts: 10
not solved ... :-/


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 10:34 am 
Beginner
Beginner

Joined: Wed Aug 10, 2005 6:21 pm
Posts: 24
Location: Antwerp, Belgium
I don't know the answer to your question but this article describes
how to make your own NHibernate Collections (in this case for databinding
in WPF). Maybe it can help you.

http://analog-man.blogspot.com/2007/01/ ... rnate.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 6:58 pm 
Newbie

Joined: Fri Jan 12, 2007 2:28 pm
Posts: 10
Location: Houston, TX
Decco - take a look at Damon Carr's implementation of IUserCollectionType here, it worked for me:

http://damon.agilefactor.com/2007/07/nhibernate-custom-collections.html

I'm sorry, it doesn't have BindingList support, but maybe you would be able to either inherit from BindingList<T> (instead of List<T> as he does).

- David McClelland


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 10:56 am 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
Hello, I tried to inherit from BindingList<T> instead of List<T> in the code proposed by drmcclelland, but I have some problems :
I guess IDomainList<T> should also inherits from IBindingList. The problem is that DomainPersistentGenericBag<T> should thus implements this interface... and I don't know how to do this...
Could anyone help me ?
I'm surprised that nobody has already written a such implementation of bindinglist...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 3:15 pm 
Beginner
Beginner

Joined: Sat Jul 21, 2007 3:56 pm
Posts: 27
You could also use a "wrapper"-strategy:

Code:
BindingList<Employee> employeeList = new BindingList<Employee>(company.Employees);


This approach will ensure that your IList stays in sync with your BindingList.

And on top of that, it keeps your entities free of GUI-related stuff (ok, I admit this is not a big argument unless you happen to be a DDD-purist :-))


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 3:48 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
this strategy is less efficient. It create a collection on each request on EmployeList.
I would rather prefer to use a sigle collection.
Could someone post an implementation of BindingList ? I'm sure it would be useful for many peoples.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 4:11 pm 
Beginner
Beginner

Joined: Sat Jul 21, 2007 3:56 pm
Posts: 27
mathmax wrote:
this strategy is less efficient. It create a collection on each request on EmployeList.


Hi mathmax,

I can understand that you don't like the fact that you have to create an additional collection, but why would it be not efficient, performance-wise? I mean, we are talking about a WinForm application, right? How many grids can you maximum have in one form?

And if you only need the collection inside your business logic, you can stick with the lighter IList.

I also wanted to start building an NHibernate-supported BindingList some time ago, but I decided to go with the "wrapper"-strategy. Maybe, if you can convince me, I can try to implement the BindingList after all :-).

Tolomaüs.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 10:53 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
So I'll try to be convincing... :-)

By less efficient, I mean at execution (of course it is not very signifiant but if you have very long collections...), but also at development. You have to write a wrapper each time you want to add a BindingListproperty....
You should also write a public property of type List that have get and set accessors for nhibernate. So the user have two properties: one of type BindingList and one of type List for the "same" collection. Quite confusing...
But the most important problem is that all event subscriptions (such as ListChanged, AddingNew) are lost on each request because you get a new BindingList instance on each time. So you loose all interest of having a BindingList instance.

If you could have a look on a BindingList implementation for nhibernate it would be nice. I'm sure that adapting the code of the above link will not be very difficult ;-)

Thanks,

mathmax


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 7:58 am 
Newbie

Joined: Wed May 30, 2007 8:18 am
Posts: 12
I also use a wrapper kind of thing to enable my datagridview controls to have access to the list of objects. This is because I want to have facility of sorting the data by clicking on the header of the grids. This required me to have binding list.

I came up with my own fix by not having collections mapped by NH at all. In my classes the collections are populated in the getter when they are accessed for the first time. During saving, I call the save method of each object in each collection of my BL class. Dirty, I know, but it works for me.

If we have this BindingList which can be directly mapped in NH I would love to remove the extra code I have to make the things clean.

I too was thinking of implementing the IBindingList in the class which inherits from GenericBag thingy, but work and family keep me away from such pleasures :)

PK

PS: I use Castle ActiveRecord (not NH directly) but I hope this discussion will benefit the user of Castle AR as well.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 10:43 am 
Beginner
Beginner

Joined: Tue Jun 06, 2006 4:47 am
Posts: 25
Haralds bindinglist solved my problems.

http://nhibernate.fluffnstuff.org/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 3:50 pm 
Beginner
Beginner

Joined: Sat Jul 21, 2007 3:56 pm
Posts: 27
mathmax wrote:
So I'll try to be convincing... :-)

By less efficient, I mean at execution (of course it is not very signifiant but if you have very long collections...), but also at development. You have to write a wrapper each time you want to add a BindingListproperty....
You should also write a public property of type List that have get and set accessors for nhibernate. So the user have two properties: one of type BindingList and one of type List for the "same" collection. Quite confusing...


Apart from having to pass via the wrapper to hand the List to the GUI, all other parts of the code stay exactly the same whether I would have a NHibernate-supported BindingList or not. I also have only a setter for the IList in my business entities and no special property for the BindingList.

So it comes down to the following difference:

with the wrapper-stategy:
Code:
            //Retrieve a list of organisations and wrap it with a BindingList
            BindingList<Organisation> organisationList = new BindingList<Organisation>(session.CreateCriteria(typeof(Organisation)).List<Organisation>());

            //Register the method to the code that should be executed when the GUI wants to create or remove an object
            organisationList.AddingNew += new AddingNewEventHandler(organisationList_AddingNew);
            organisationList.RemovingItem += new EventHandler<RemovingItemEventArgs>(organisationList_RemovingItem);

            //Hand the IBindinglist over to the GUI
            organisationBindingSource.DataSource = organisationList;


with a NHibernate-aware BindingList:

Replace the first line with:
Code:
            BindingList<Organisation> organisationList = session.CreateCriteria(typeof(Organisation)).List<Organisation>();


Quote:
But the most important problem is that all event subscriptions (such as ListChanged, AddingNew) are lost on each request because you get a new BindingList instance on each time. So you loose all interest of having a BindingList instance.
mathmax


Wouldn't you do this anyway? Maybe I don't get your point. Could you provide an example of such a scenario?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 8:15 pm 
Regular
Regular

Joined: Tue Jun 26, 2007 11:50 am
Posts: 105
I'm not sure to understand the "wrapper" strategy.

I would have write this :

public class Test
{
private List<string> _MyList = new List<string>();

public List<string> MyListForNhibernate
{
get { return _MyList; }
set { _MyList = value; }
}

public BindingList<string> MyList
{
get { return new BindingList<string>(_MyList);}
set { _MyList = new List<string>(value); }
}
}

So :

MyListForNhibernate : would just be use by nhibernate to load/save the collection from the database.

MyList : Would be used by the user.

By this way, the user create a new BindingList<string> instance on each request on MyList. So events are lost each time you get MyList.

I don't see where to put your above code. Perhaps you could give me a short example of this strategy so that I understand how it works. Or perhaps there is an article on the web that explains this strategy ?

stovesy, I looked at the Nhibernate.Databinding extension. It provides a custom BindindList<T> but no interface IBindindList<T>. Nhibernate requires to work with interface, doesn't it ? So how to use this BindingList implementation ?

Many thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 22, 2007 4:19 am 
Beginner
Beginner

Joined: Tue Jun 06, 2006 4:47 am
Posts: 25
Mathmax

The list property in the parent entity is defined as:-
Code:
private NHibernate.DataBinding.BindingList<yourlisttype> _items = BindingList.GetInstance<yourlisttype>();

public virtual NHibernate.DataBinding.BindingList<yourlisttype> items


and then the hibernate configuration is:-
Code:
<list name="items" table="yourchildtable" generic="true" access="NHibernate.DataBinding.BindingAccessor, NHibernate.DataBinding">
   <key column="youtkeycolumn"/>
   <index column="index"/>
   <one-to-many class="yourchildclass"/>
</list>


After that, any collections can be bound to quite simply.
All works perfectly for me.

Some folks may also be interested in a winforms control called XPTable (http://sourceforge.net/projects/xptable/).
It's a list type control which has been extended with databinding capabilities.
It also has a virtual ability, so any lazy loaded information in a list is not populated by the table until it's needed.

It's currently being developed, but works really well in a NHibernate environment.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.