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.  [ 2 posts ] 
Author Message
 Post subject: IUserCollectionType, a Generic implementation
PostPosted: Thu Nov 09, 2006 6:31 am 
Beginner
Beginner

Joined: Wed Mar 22, 2006 6:59 am
Posts: 30
Hi friends !

IUserCollectionType is really cool ! I saw in source Tests how to use it, but ( as i´m kind of new to .NET) i ask for some Generic <> implementation of it... if someone could post it would be greate.

Thanks !

PS.: sorry for my bad english


Top
 Profile  
 
 Post subject: Implementation of NHibernate Custom Collection - Lazy Works!
PostPosted: Sun Jul 22, 2007 3:56 pm 
Newbie

Joined: Fri Sep 08, 2006 5:30 pm
Posts: 4
You can download this code from my blog:

damon.agilefactor.com soon...

Here is the trick (non-lazy is easy enough but the trick is getting Lazy to work):



First the overview (this is for a general audience as well so pardon the explicit explanations as I realize most of you are gurus here):

As I think many on this list know to have any kind of custom persistable collection you must inherit from PersistentGenericBag<T> which has no empty constructors (therefore you cannot use this to inherit on your main collection (!) as you MUST have an empty constructor for NHibernate to use).

We want our main custom collection to inherit from the concrete List<T> to use its core services as our base so you have already used your 1 concrete parent. (if we had multiple inheritance we could inherit from both, problem solved - kind of - almost - well another day.....).

Our interface must inherit from multiple sources for casting to work, as we need it to:

a) It must inherit from IList<T> as that is our baseline 'lowest common denominator' collection type

b) ALSO....(I see people report this as an error fairly often) for casting purposes, we need to be able to cast our item to PersistentGenericBag<T> so we must also inherit from the same interface. This crashes for people all the time who diligently create custom collections. Once you get Lazy, BAM1 Casting Exception. This solves that problem.

What is so tricky? PersistentGenericBag<T> HAS NO INTERFACE we can use!


Solution (basic OO Knowledge really): Extract the interface (I live and die by ReSharper)...Fake out the system by using a concrete class of your own instead of PersistentGenericBag<T> which both inherits from PersistentGenericBag<T> and implements the extracted interface. Then only refer to our 'wrapper' class.

So there are 3 files:

1) internal class DomainPersistentGenericBag<T> : PersistentGenericBag<T>, IDomainList<T>


2) public interface IDomainList<T> : IList<T>


NOTE: This interface contains our extracted interface which has every single public item from DomainPersistentGenericBag<T> and a 'new' prefix on items where we override the IList<T>'s method.

NOTE2: I started by calling this IDomainPersistentGenericBag. This is perhaps more 'correct' but not as easy for the developers to sync the Interface and the concrete class as they do with IList and List (you'll see).


3) Finally our main Collection class, which we use all over the place quite easily:

public class DomainList<T> : List<T>, IUserCollectionType, IDomainList<T>


All the code is downloable from the blog below....


To use this simply:

1) In your Domain Class (I never use mapping files now, only attributes FYI):

public class MyDomainClass() {

...


private IDomainList<ArticleZones> _Zones;


[Bag(0, Inverse = true, CollectionTypeType = typeof(DomainList<ArticleZones>), Lazy = true, Generic = true, BatchSize = 10)]
[Key(1, Column = "articleid")]
[OneToMany(2, Class = "PMSiteDomainModel.ArticleZones, PMSiteDomainModel")]
public virtual IDomainList<ArticleZones> Zones
{
get { return _Zones; }
protected set { _Zones = value; }

}


Works like a charm!

You can refer to this collection as:

IDomainList<T> or DomainList<T>
IList<T> or List<T>

All work and you get full customization!

To download the FULL code, this will shortly be up on my blog as well as more details here:

http://damon.agilefactor.com



Thanks,
Damon Carr, CTO
agilefactor


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

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.