在 DAO 中反映继承关系最有效的方法是什么?

发布于 2024-10-16 21:27:58 字数 405 浏览 6 评论 0原文

使用 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 技术交流群。

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

发布评论

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

评论(2

Martin Fowler 记录了一些可能有帮助的对象关系结构模式

1) 单表继承:将类的继承层次结构表示为单个表,该表包含各个类的所有字段的列。

例如,EmployeeCustomer都继承自User,并且都存储在User表中,其中有一列确定特定记录的用户类型代表。

2) 类表继承:表示类的继承层次结构,每个类有一个表。

例如,EmployeeCustomer 都继承自User,并且有三个表来表示这一点。用户表存储所有用户共有的属性。 Employee表有一个指向User表的指针,并且只存储与Employee相关的属性。 Customer 表也是如此。

3) 具体表继承:表示类的继承层次结构,层次结构中每个具体类都有一个表。

例如,EmployeeCustomer 都继承自抽象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 and Customer both inherit from User 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 and Customer both inherit from User 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 and Customer both inherit from the abstract User 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.

心如荒岛 2024-10-23 21:27:58

您可以在 CustomerDAO 中拥有 UserDAO 的实例。您需要与 User 和 Customer 表进行交互以覆盖基类和子类列。例如:

public class CustomerDAO
{
    UserDAO userDao;

    // ... initialization ...

    public void update(Customer customer)
    {
        // ... first execute update of customer table ...

        userDao.update(customer);   // Should be able to pass Customer to UserDAO due to inheritance
    }

    public void insert(Customer customer)
    {
        // First insert a row in the User table so that the ID of the user can be determined.
        userDao.insert(customer);

        // ... Now execute insertion of row into Customer table ...
    }

    public void delete(Customer customer)
    {
        // ... first delete Customer row ...

        // Now delete base class User row.
        userDao.delete(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:

public class CustomerDAO
{
    UserDAO userDao;

    // ... initialization ...

    public void update(Customer customer)
    {
        // ... first execute update of customer table ...

        userDao.update(customer);   // Should be able to pass Customer to UserDAO due to inheritance
    }

    public void insert(Customer customer)
    {
        // First insert a row in the User table so that the ID of the user can be determined.
        userDao.insert(customer);

        // ... Now execute insertion of row into Customer table ...
    }

    public void delete(Customer customer)
    {
        // ... first delete Customer row ...

        // Now delete base class User row.
        userDao.delete(customer);
    }
}

Of course, each Customer operation should probably be wrapped in a transaction so that updating the User and Customer tables does not fail independently.

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