NHibernate on .Net 2.0Part II
My first attempt in using generic collections in NHibernate was to just make NHibernate grok my custom collection. I took a look in the code, and it seemed rather straightforward to do so. I posted to the developer list and got the following response from Sergey, the lead developer:
- Have your collection class implement an interface (class MyCollection: IMyCollection)
- Create NHibernate-specific wrapper class (PersistentMyCollection : PersistentCollection, IMyCollection) which functions as a proxy for the real collection. All methods from IMyCollection that PersistentMyCollection implements have a Read() or Write() at the beginning, and then delegate to the real collection. PersistentMyCollection should also implement some abstract methods from PersistentCollection, these mostly have to do with snapshots and loading process.
- Create MyCollectionType which will derive from PersistentCollectionType and implement methods like Wrap (wrapping MyCollection in PersistentMyCollection), Instantiate, Add and Clear, and the rest.
- Modify the binder to accept your collection type, and that should be it.
It's straightforward but a long process, and it involves some deep knowledge about the way NHibernate works. Luckily I managed to get it most of it pre-baked by transforming the existing Set collections to use generics. I started to run into problems when I had to dynamically construct generic types, but this article sums it pretty well. I could get the correct info and get it to work.
I decided to wait with that for now, and try a less invasive approach. I'm currently trying to get it to work using a Property Accessors. I'll post my results when I'm done.
More posts in "NHibernate on .Net 2.0" series:
- (01 Oct 2005) Part IV - The Sucess
- (01 Oct 2005) Part III
- (30 Sep 2005) Part II
- (30 Sep 2005) Part I
Comments
Comment preview