-->
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.  [ 4 posts ] 
Author Message
 Post subject: Adding methods to NHibernate domain model collections
PostPosted: Fri Apr 03, 2009 10:42 am 
Newbie

Joined: Fri Apr 03, 2009 10:36 am
Posts: 6
I've been building an NHibernate domain model from our legacy database for a few days now, building out the domain model with methods - I hate anemic domain models.

But I've yet to work out how to add methods to ISet or Collection class instances created by NHibernate.

We have a legacy handbuilt domain model which would be nice to replace with NHibernate and the collection classes have methods such as Merge() or properties like AllSuccessful. How do I give the NHibernate domain model the same domain methods?


Thanks,
Richard
ps I'd rather not hear any answer containing the phrase "Extension Method"...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 12:49 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can wrap the hibernate collections in a component which represents your enhanced lcollections:

Code:
<component name="Items" class="MyEnhancedList">
      <bag name="Items" table="..." ...>
        <key column="..."/>
        <one-to-many class="MyItemClass"/>
      </bag>
</component>


Items is a property in your collection implementation that contains the items:

Code:
    public abstract class ListBase<T> : IList<T>, IList where T : class
    {
        private IList<T> items;

        protected ListBase()
        {
            items = new List<T>();
        }

        public IList<T> Items
        {
            get { return items; }
            protected set {items = value; }
        }
...
}


The methods necessary for IList can simply be passed to the inner list. Any additional functionality can go in the wrapper class.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 04, 2009 2:58 am 
Newbie

Joined: Fri Apr 03, 2009 10:36 am
Posts: 6
Thanks. That's an ingenious idea. I suppose that would break any Linq to NHibernate support, but I'll definitely investigate it.

Fluent NHibernate implies it's possible to specify a custom collection type here:

http://wiki.fluentnhibernate.org/show/S ... ctionTypes

How does this work behind the scenes? What's the equivalent in .hbm files?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 04, 2009 3:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can implement your own collections. You have to implemnt IUserCollectionType on your list. But I've never used that before, so I can't help you there. And if you want to use lazy loading, you'll probably have a lot of work.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.