How to model this situation in ORM (NHibernate)?

江枫思渺然 提交于 2020-01-13 07:08:14

问题


I've always taken a data centric approach to web apps, and so the paradigm change involved when taking a pure OO approach using ORMs is still not completely natural to me, and evey now and then something ostensibly simple is not obvious.

Can someone please point me into the right direction of how to correctly model this situation in an ORM (Specifically NHibernate).

The situation: There are many Users, Many Items, users may rate Items.

In a data centric/relational approach it would look like this (and be very easy!):

- Items Table
ItemID 
ItemName

- UserRatings Table
UserID 
ItemID 
Rating

- Users Table
UserID 
UserName

If you want to find out what rating your logged in user gave for a particular item, you just do a joined query.

However, it's not obvious to me how to model this in a OO way when using an ORM. I beleive I need three domain classes: Item, UserRating, and User. Presumably I also need a collection of UserRatings in the Items class, and a collection of UserRatings in the User class. But how do I navigate to the corect UserRating, from a particular item which I have loaded (and of course this UserRating must be the one related to the user I'm interested in).

Can anyone clarify this for me?

Thanks


回答1:


Your classes would look like the following

public class User 
{
    public virtual int UserId { get; set; }
    public virtual string UserName { get; set; }
    public virtual IList<UserRating> Ratings { get; set; }
}

public class Item 
{
    public virtual int ItemId { get; set; }
    public virtual string ItemName { get; set; }
    public virtual IList<UserRating> Ratings { get; set; }
}
public class UserRating 
{
    public virtual User User { get; set; }
    public virtual Item Item { get; set; }
    public virtual Int32 Rating { get; set; }
}

Your mapping file would look like

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Test" namespace="Test" >
    <class name="User">
        <id name="UserId" >
            <generator class="native" />
        </id>
        <property name="UserName" />
        <bag name="Ratings" generic="true" inverse="true" table="UserRating">
            <key column="UserId" />
            <one-to-many class="UserRating"/>
        </bag>
    </class>
    <class name="Item" >
        <id name="ItemId" >
            <generator class="native" />
        </id>
        <property name="ItemName" />
        <bag name="Ratings" generic="true" inverse="true" table="UserRating">
            <key column="ItemId" />
            <one-to-many class="UserRating"/>
        </bag>
    </class>
    <class name="UserRating" >
        <composite-id>
            <key-many-to-one class="User" column="UserId" name="User" />
            <key-many-to-one class="Item" column="ItemId" name="Item" />
        </composite-id>
        <property name="Rating" />
    </class>
</hibernate-mapping>

I would suggest you look at the Summer of Nhibernate screen cast as well as the Autumn of Agile series by Stephen Bohlen which gave me a great start with nhibernate.



来源:https://stackoverflow.com/questions/1679332/how-to-model-this-situation-in-orm-nhibernate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!