如何在ORM(NHibernate)中为这种情况建模? [英] How to model this situation in ORM (NHibernate)?

查看:54
本文介绍了如何在ORM(NHibernate)中为这种情况建模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直对Web应用程序采取以数据为中心的方法,因此,使用ORM进行纯OO方法时涉及的范式更改对我来说仍然不完全自然,并且时不时地发出一些表面上看起来很简单的东西.

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.

有人可以向我指出如何在ORM(尤其是NHibernate)中正确建模这种情况的正确方向.

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.

但是,对我而言,使用ORM时如何以OO方式对此建模并不明显.我相信我需要三个域类:Item,UserRating和User.大概我还需要Items类中的UserRatings集合,以及User类中的UserRatings集合.但是,如何从已加载的特定项目导航到核心的UserRating(当然,该UserRating必须是与我感兴趣的用户相关的项目).

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?

谢谢

推荐答案

您的类如下所示

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; }
}

您的映射文件看起来像

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

我建议您查看 Nhibernate的夏季屏幕投射以及

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.

这篇关于如何在ORM(NHibernate)中为这种情况建模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆