在 DAO 中反映继承关系最有效的方法是什么?
使用 MVC 结构和 业务对象 / DAO 架构。对于任何正常的业务对象,CRUD 功能都相当简单。但处理“客户就是用户”这样的亲子关系最好的方式是什么?
我知道涉及以下类:
User, UserDAO, Customer, CustomerDAO
Customer
类可以很好地继承 User
,但是如何在 DAO CRUD 函数中最好地反映这一点?
Working on a business application using MVC structure and a Business Object / DAO architecture. For any normal business object, the CRUD functions are fairly straightforward. But what's the best way to handle a parent-child relationship like "Customer is a User"?
I know that the following classes are involved:
User, UserDAO, Customer, CustomerDAO
The Customer
class can inherit from the User
just fine, but how do you best reflect this in the DAO CRUD functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Martin Fowler 记录了一些可能有帮助的对象关系结构模式:
1) 单表继承:将类的继承层次结构表示为单个表,该表包含各个类的所有字段的列。
例如,
Employee
和Customer
都继承自User
,并且都存储在User表中,其中有一列确定特定记录的用户类型代表。2) 类表继承:表示类的继承层次结构,每个类有一个表。
例如,
Employee
和Customer
都继承自User
,并且有三个表来表示这一点。用户表存储所有用户共有的属性。 Employee表有一个指向User表的指针,并且只存储与Employee相关的属性。 Customer 表也是如此。3) 具体表继承:表示类的继承层次结构,层次结构中每个具体类都有一个表。
例如,
Employee
和Customer
都继承自抽象User
类,并且有两个表来表示这一点。一个客户表和一个员工表。每个表存储用户共有的信息,但也存储独特的属性。Martin Fowler has documented a few Object-Relational Structural Patterns that might help:
1) Single Table Inheritance: Represents an inheritance hierarchy of classes as a single table that has columns for all the fields of the various classes.
e.g.
Employee
andCustomer
both inherit fromUser
and are both stored in the User table, with a column that determines what type of user a particular record represents.2) Class Table Inheritance: Represents an inheritance hierarchy of classes with one table for each class.
e.g.
Employee
andCustomer
both inherit fromUser
and there are three tables to represent this. The User table stores the properties common to all users. The Employee table has a pointer to the User table and only stores the properties relevant to Employees. The same is true of the Customer table.3) Concrete Table Inheritance: Represents an inheritance hierarchy of classes with one table per concrete class in the hierarchy.
e.g.
Employee
andCustomer
both inherit from the abstractUser
class and there are two tables to represent this. A Customer table and an Employee table. Each table stores information common to users, but also stores unique properties.您可以在 CustomerDAO 中拥有 UserDAO 的实例。您需要与 User 和 Customer 表进行交互以覆盖基类和子类列。例如:
当然,每个 Customer 操作可能应该包装在一个事务中,以便更新 User 和 Customer 表不会单独失败。
You can have an instance of UserDAO in CustomerDAO. You need to interact with both the User and Customer tables to cover the base class and subclass columns. For example:
Of course, each Customer operation should probably be wrapped in a transaction so that updating the User and Customer tables does not fail independently.