获取 Table-per-Hierarchy 结构的 DAO
假设我有一个 User
类和两个子类 Employee
和 Customer
。我将此层次结构实现为数据库中的每个层次结构表,并有一列用于指定用户类型。查询该表时我需要返回正确类型的对象。
我是否需要为每种类型的对象(例如 CustomerDAO
或 EmployeeDAO
)提供单独的 DAO,以便每个对象返回各自的 Customer
和 Employee
对象。如果是这样,如何从 DAOFactory
获取它们而不使用:
if(type.equlas('customer'))
return customerDao;
else
retrun employeeDao;
因为实现 User
的类型可能会改变,我不想每次都改变条件。
或者还有其他办法吗?任何想法将不胜感激。
注意:我没有使用任何 ORM 框架,也不打算使用。
Let's assume I have a User
class and two subclasses Employee
and Customer
. I implemented this hierarchy as a table-per-hierarchy in DB with a column for specifying the type of user. I need to return the right type of object when querying this table.
Do I need separate DAOs for each type of object like CustomerDAO
or EmployeeDAO
, so each return their respective Customer
and Employee
objects. If so how to get them from DAOFactory
without using:
if(type.equlas('customer'))
return customerDao;
else
retrun employeeDao;
Because the types implementing User
could change and I don't want to change conditions every time.
Or is there any other way? Any idea would be appreciated.
Note: I'm not using any ORM framework and not planning to use one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果每种类型的持久性代码相同,那么您可以拥有 1 个通用 DAO。
所以你的用户 dao 可能是这样的:
然后你的工厂类可以通过指定正确的类型来实例化正确的 DAO。
问候
优素福
If your persistence code for each type is the same, then you could have 1 generic DAO.
So your user dao, could be something like:
Then your factory class could instantiate the correct DAO simply by specifying the correct type.
Regards
Yusuf