0

我正在尝试使用 Nhibernate 作为我的 ORM 的社交网络类型场景。

我有一个用户表和一个朋友表。

User Table
----------
UserID
FirstName
LastName
Email

Friend Table
-------------
UserID
FriendUserID
AddedDateTime

这意味着在朋友表中它可能有很多记录,比如

A - B (A initiated a friend request to B, accepted)
A - C (A initiated a friend request to C, accepted)
B - C
D - A (D initiated a friend request to A, accepted)
B - E

我怎样才能拥有一个 Nhibernate 属性,这样如果我为 A 调用 userObject.Friends,我将 B、C 和 D 作为用户对象返回。

为用户对象映射 xml...

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Namespace.Data.Entities.User,Namespace.Data.Entities" table="`User`" lazy="true">
    <id name="UserId" column="`UserId`" type="Guid">
      <generator class="assigned" />
    </id>
    <property type="string" length="150" name="FirstName" column="`FirstName`" />
    <property type="string" length="150" name="LastName" column="`LastName`" />
    <property type="string" not-null="true" length="256" name="Email" column="`Email`" />    
    <bag name="UsersFriends" inverse="true" lazy="true" cascade="all">
      <key column="`UserID`" />
      <one-to-many class="Namespace.Data.Entities.Friend,Namespace.Data.Entities" />
    </bag>
    <bag name="FriendsofUser" inverse="true" lazy="true" cascade="all">
      <key column="`FriendUserID`" />
      <one-to-many class="Namespace.Data.Entities.Friend,Namespace.Data.Entities" />
    </bag>
  </class>
</hibernate-mapping>

和朋友对象...

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Namespace.Data.Entities.Friend,Namespace.Data.Entities" table="`Friend`" lazy="true">
    <id name="Id" column="`ID`" type="int">
      <generator class="native" />
    </id>
    <property type="DateTime" not-null="true" name="AddedDateTime" column="`AddedDateTime`" />
    <many-to-one name="User" cascade="none" column="`UserID`" not-null="true" />
    <many-to-one name="FriendUser" cascade="none" column="`FriendUserID`" not-null="true" />
  </class>
</hibernate-mapping>
4

1 回答 1

2

我会建议

public class User
{
    public virtual long Id { get; set; }
    public virtual ICollection<FriendshipRequest> InitiatedRequests { get; private set; }
    public virtual ICollection<FriendshipRequest> ReceivedRequests { get; private set; }
    public virtual IEnumerable<User> Friends
    {
        get {
            return  (from fr in InitiatedRequests
                     where fr.Accepted
                     select fr.Friend)
                    .Concat(
                     from fr in ReceivedRequests
                     where fr.Accepted
                     select fr.Initiator);
        }
    }

    public User()
    {
        InitiatedRequests = new HashSet<FriendshipRequest>();
        ReceivedRequests = new HashSet<FriendshipRequest>();
    }
}

public class FriendshipRequest
{
    public virtual User Initiator { get; set; }
    public virtual User Friend { get; set; }
    public virtual bool Accepted { get { return Added != default(DateTime); } }
    public virtual DateTime Added { get; set; }

    // override Equals
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Namespace.Data.Entities.User,Namespace.Data.Entities" table="`User`">
    ...
    <set name="InitiatedRequests" cascade="all" table="FriendshipRequests">
      <key column="`UserID`" />
      <composite-element>
        <property type="DateTime" name="Added" column="`AddedDateTime`" />
        <parent name="Initiator" />
        <many-to-one name="Friend" column="`FriendUserID`" not-null="true" />
      </composite-element>
    </bag>
    <set name="ReceivedRequests" cascade="all" table="FriendshipRequests" inverse="true">
      <key column="`FriendUserID`" />
      <composite-element>
        <property type="DateTime" name="Added" column="`AddedDateTime`" />
        <parent name="Friend" />
        <many-to-one name="Initiator" column="`UserID`" not-null="true" />
      </composite-element>
    </bag>
  </class>
</hibernate-mapping>
  • 对于 ReceivedRequests 集合,逆必须设置为 true,以便仅从一侧处理此集合
  • 我会设置,所以每个友谊只能添加一次(哪些属性是唯一的取决于相等),这是有道理的,NHibernate 可以优化一些东西
  • 为避免选择 n+1,如果您打算使用朋友收藏,请确保您也渴望加载其他收藏
于 2012-05-31T13:18:00.283 回答