NHibernate:从单个表行映射多个类

发布于 2024-08-28 00:01:34 字数 830 浏览 5 评论 0原文

我找不到这个具体问题的答案。我试图保持我的领域模型面向对象,并尽可能重用对象。我在确定如何从单行提供到多个类的映射时遇到问题。让我用一个例子来解释:

我有一个表,称之为“客户”。客户有多种属性;但是,为了简洁起见,假设它具有 ID、名称、地址、城市、州、邮政编码。

我想创建一个如下所示的 Customer 和 Address 类:

public class Customer {
    public virtual long Id {get;set;}
    public virtual string Name {get;set;}
    public virtual Address Address {get;set;}
}

public class Address {
    public virtual string Address {get;set;}
    public virtual string City {get;set;}
    public virtual string State {get;set;}
    public virtual string ZipCode {get;set;}
}

我遇到的问题是确定 Customer 类中的 Address 类的映射是什么。没有地址表,也没有与客户关联的地址“集”。我只想在代码中获得更面向对象的 Customer 表视图。还有其他几个表中包含地址信息,如果有一个可重用的 Address 类来处理它们就好了。地址不共享,因此将所有地址分成带有外键的单独表似乎有点过分,实际上,更痛苦,因为我需要多个表的外键。

有人可以启发我这种类型的映射吗?如果可以的话请提供一个例子。

感谢您的任何见解!

-麦克风

I couldn't find an answer to this specific question. I am trying to keep my domain model object-oriented and re-use objects where possible. I am having an issue determining how to provide a mapping to multiple classes from a single row. Let me explain with an example:

I have a single table, call it Customer. A customer has several attributes; but, for brevity, assume it has Id, Name, Address, City, State, ZipCode.

I would like to create a Customer and Address class that look like this:

public class Customer {
    public virtual long Id {get;set;}
    public virtual string Name {get;set;}
    public virtual Address Address {get;set;}
}

public class Address {
    public virtual string Address {get;set;}
    public virtual string City {get;set;}
    public virtual string State {get;set;}
    public virtual string ZipCode {get;set;}
}

What I am having trouble with is determining what the mapping would be for the Address class within the Customer class. There is no Address table and there isn't a "set" of addresses associated with a Customer. I just want a more object-oriented view of the Customer table in code. There are several other tables that have address information in them and it would be nice to have a reusable Address class to deal with them. Addresses are not shared so breaking all addresses into a separate table with foreign keys seems to be overkill and, actually, more painful since I would need foreign keys to multiple tables.

Can someone enlighten me on this type of mapping? Please provide an example if you can.

Thanks for any insights!

-Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2024-09-04 00:01:34

您应该在 NHibernate 映射文件中使用组件。
例如:

<class name="Customer"
table="Customer">

<id name="Id">
    <generator class="identity"/>
</id>
<property name="Name" />
<component name="Address">
    <property name="Address"/>
    <property name="City"/>
    <property name="State"/>
    <property name="ZipCode"/>
</component>
</class>

可以在 Ayende 的博客< /a>.

You should use a Component in your NHibernate mapping file.
For example:

<class name="Customer"
table="Customer">

<id name="Id">
    <generator class="identity"/>
</id>
<property name="Name" />
<component name="Address">
    <property name="Address"/>
    <property name="City"/>
    <property name="State"/>
    <property name="ZipCode"/>
</component>
</class>

More can be read about it in Ayende's blog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文