NHibernate:从单个表行映射多个类
我找不到这个具体问题的答案。我试图保持我的领域模型面向对象,并尽可能重用对象。我在确定如何从单行提供到多个类的映射时遇到问题。让我用一个例子来解释:
我有一个表,称之为“客户”。客户有多种属性;但是,为了简洁起见,假设它具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 NHibernate 映射文件中使用组件。
例如:
可以在 Ayende 的博客< /a>.
You should use a Component in your NHibernate mapping file.
For example:
More can be read about it in Ayende's blog.