简体   繁体   English

在MVP设计模式中,谁负责访问数据库

[英]In a MVP design pattern, who is responsible for accessing the database

I have a question related to MVP design pattern. 我有一个与MVP设计模式有关的问题。

I have a View that passes to the presenter all events. 我有一个视图,可以将所有事件传递给演示者。 The presenter has the bussiness logic and updates the view. 演示者具有业务逻辑并更新视图。 The model is just a collection of JPA entities . 该模型只是JPA实体的集合。

The question is, who is responsible for accessing the database and retrieving the model? 问题是,谁负责访问数据库和检索模型?

My first thought is that this is a presenter responsibility. 我的第一个想法是,这是主持人的责任。 BUT, imagine that the same business logic is necessary in several pages, for example, verify if a user has permission to access something. 但是,假设在多个页面中需要相同的业务逻辑,例如,验证用户是否有权访问某些内容。 In this case, the business logic would appear in several presenter classes, which is not good. 在这种情况下,业务逻辑将出现在几个presenter类中,这是不好的。

What would be the best implementation? 最好的实现方式是什么?

It sounds like you're interested in the differences between MVP and MVC . 听起来您对MVPMVC之间的差异感兴趣。 Note that MVP is a newer variation of the classic MVC architecture, with a key difference being who accesses the model (see the differing diagrams in the Wikipedia articles). 请注意,MVP是经典MVC体系结构的更新版本,主要区别在于访问模型的人(请参阅Wikipedia文章中的不同图表)。

In the classic MVC pattern, both the View and Controller access the model, whereas only the Presenter has access in MVP. 在经典MVC模式中,视图和控制器都可以访问模型,而只有Presenter可以访问MVP。 Also note that in MVP, not all business logic is necessarily located in the Presenter. 另请注意,在MVP中,并非所有业务逻辑都必须位于Presenter中。 From Wikipedia, " The degree of logic permitted in the view varies among different implementations... the view... may be the best place to handle a particular interaction or command. " 在Wikipedia中,“ 视图中允许的逻辑程度在不同的实现方式中有所不同...视图...可能是处理特定交互或命令的最佳位置。

The answer to your second question is, of course, "It depends on what you're implementing." 第二个问题的答案当然是“这取决于您要实现的内容”。 If you think that MVP will result in duplicated logic in the Presenter of your particular application, consider moving some logic to the View, or consider switching to the MVC pattern. 如果您认为MVP将导致特定应用程序的Presenter中的逻辑重复,请考虑将某些逻辑移至View,或考虑切换至MVC模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM