Nhibernate映射 - 用户和朋友

时间:2012-05-29 22:44:09

标签: c# nhibernate nhibernate-mapping

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

我有一个User表和一个Friend表。

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>

1 个答案:

答案 0 :(得分: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必须将inverse设置为true,以便此集合仅从一侧处理
  • 我会接受设置所以每个友谊只能添加一次(哪些属性是唯一的取决于等于),这是有道理的,NHibernate可以优化一些东西
  • 避免选择n + 1,如果您打算使用朋友收藏,请确保您也急切加载其他收藏品