Well, the ORM works by creating a representation of what's in the database,
which allows you to work with persistence ignorance on the object model, and
that includes adding and removing elements from a collection, which is then
mapped to inserting and deleting records from a DB table.

Now, there *IS* an alternative to what I proposed that might be more aligned
with your original idea.
Instead of loading the collection, you could try to get the record from the
Friends table which represents the relationship. However, in order to do
that, you have to change your domain model and usage to adjust to that
particular use case, which is what you should avoid.

There are always tradeoffs with ORMs, but the benefits largely are way
bigger than the costs.

   Diego


On Wed, Nov 25, 2009 at 13:08, Kevin Pang <[email protected]> wrote:

> Diego,
>
> Perfect.  That's exactly what I was looking for.  Thanks! :-)
>
> When you say that you *have* to load the collection, I assume you mean
> that since we're dealing with an ORM the default approach should be to
> load collections into memory so that you can perform business logic on
> it?  For instance, if this business logic were something more complex
> than uniqueness and depended on more properties within the collection,
> then loading it and traversing it would be the best approach as
> opposed to directly hitting the database with a query?
>
> Kevin
>
> On Nov 25, 2:22 am, Diego Mijelshon <[email protected]> wrote:
> > First... you have a collection where items are unique, and order doesn't
> > matter, so use a <set> instead of a <bag> (map it to a ICollection<User>
> or
> > ISet<User>). That should take care of uniqueness.
> >
> > Second... this is an ORM, so you *have* to load the collection (yes, you
> can
> > use DML-syle operations<
> http://www.nhforge.org/doc/nh/en/index.html#batch-direct>,
> > but it defeats the purpose if you do it all the time).
> > However, there's something that can help you. This is the mapping I'd
> use:
> >
> >  <class name="User" table="Users"> <id column="UserId" type="int">
> > <generator class="identity"/> </id> <!--other properties...--> <set
> > name="Friends" table="Friends" lazy="true"> <key column="UserId"/>
> > <many-to-many class="User" column="FriendId" fetch="select"/> </set>
> > </class>
> >
> > That fetch="select" property tells NHibernate to use proxies for the
> set's
> > elements, so you'll just be loading the friends' primary keys (from the
> > Friends table, no join) instead of the whole User object.
> >
> > You can then do this:
> >
> > var user = session.Get<User>(id);
> > var friend = session.Load<User>(friendId);
> > user.Friends.Add(friend);
> > user.Friends.Add(friend); //does nothing
> >
> > Voila... That was one select for the user, one for the collection.
> > Flushing at that point would issue one insert (the friend object wasn't
> even
> > retrieved from the DB)
> >
> > Hope this helps.
> >
> >    Diego
> >
> >
> >
> > On Wed, Nov 25, 2009 at 06:16, Kevin Pang <[email protected]> wrote:
> > > Let's say my system allows users to specify other users as "friends".
> > > The database tables would look something like this:
> >
> > > Users
> > > - UserId
> >
> > > Friends
> > > - UserId
> > > - FriendId
> >
> > > The most logical way for me to map this, in my opinion, is using a
> > > <bag> with a <many-to-many> so that my User class looks like this:
> >
> > > public class User
> > > {
> > >    public virtual IList<User> Friends {get; set;}
> > > }
> >
> > > Now, if I want to enforce a rule so that you can't add a friend that
> > > you already have as a friend, I might do it like this:
> >
> > > public virtual void AddFriend(User user)
> > > {
> > >    foreach (User friend In Friends)
> > >    {
> > >        if (friend.UserId == user.UserId)
> > >            return; // Friend already exists, don't add
> > >    }
> >
> > >    Friends.Add(user);
> > > }
> >
> > > This all seems perfectly reasonable from a domain perspective.  My
> > > only problem with this is the foreach loop inside AddFriend will
> > > trigger a call to the database to load up all of the user's friends.
> > > This seems excessive since all I really care about is whether there
> > > exists one row with a specific UserId and FriendId.
> >
> > > How would you model this?
> >
> > > --
> >
> > > You received this message because you are subscribed to the Google
> Groups
> > > "nhusers" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.
>
> --
>
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
>

--

You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.


Reply via email to