数据访问对象方法和论点
对于数据访问对象,有哪些方法和参数?典型的 DAO 看起来会像这样
DAO<T>
+ select(): List<T>
+ selectOne(id: int): T
+ insert(obj: T): boolean
+ update(obj: T): boolean
+ delete(obj: T): boolean
吗?如果是的话,为什么不让我的实体/域对象从此类继承呢?然后我就不必将对象传递到此类中。目前,这个类似乎会包含很多类似的代码?我想我对 DAO 的理解有点错误?我对 ORM 比较熟悉
For a Data Access Object, what methods and arguments are there? Will a typical DAO look like
DAO<T>
+ select(): List<T>
+ selectOne(id: int): T
+ insert(obj: T): boolean
+ update(obj: T): boolean
+ delete(obj: T): boolean
If so then why not just have my Entities/Domain Objects inherit from this class? Then I wont have to pass in objects into this class. Also currently, this class seems like it will contain alot of similar code? I suppose my understanding of DAO are abit wrong? I am more familiar with ORMs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,典型的 DAO 与您所描述的非常相似,并且有一些库可以帮助您避免创建仅类型不同的类似方法(如果您使用 ORM 实现 DAO,请查找通用 DAO)。
例如,像 Spring-Data-JPA 这样的框架可以让您专注于通过其他条件查询对象的不常见方法。
将这些方法移至实体对象是一些人喜欢的模式,但正是能够定义通用 DAO 并从中继承它的点这就是将此方法留在不同类中的原因。
所以,我不认为你对 DAO 的理解是错误的。
Indeed, typical DAOs are a lot like what you describe and there are libraries that help you avoid to create similar methods that differ only in type (if you implement the DAO's with an ORM, look for generic DAOs).
Frameworks like Spring-Data-JPA, for instance, let you focus on the uncommon methods of querying objects by other criteria.
Moving those methods to the entity objects is a pattern that some people prefer but precisely the point of being able to define a generic DAO and inherit from it it would be a reason to leave this methods in different classes.
So, I don't you are wrong in your understanding of DAOs.