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.