<< This all seems perfectly reasonable from a domain perspective.  >>

I don't think you should use the OOID that explicitly in your domain. Use...
If(!Friends.Contains(user))
  Friends.Add(user);
...or similar and define what "equals" mean elsewhere.

Anyhow, that won't solve your problem. Don't know if this was more of a general 
question or if you just want to make sure no duplicates exists in a colletion? 
If the later, use <set> instead of <bag> and just add the user to the friend 
collection.

/Roger

  

-----Original Message-----
From: Kevin Pang [mailto:[email protected]] 
Sent: den 25 november 2009 10:17
To: nhusers
Subject: [nhusers] How would you map this?

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].
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