简体   繁体   English

理解模型视图演示(MVP)

[英]Understanding Model View Presentation (MVP)

I have searched a lot for MVP, but I haven't found any good articles that help me understand it very well. 我已经搜索了很多MVP,但我没有找到任何有助于我理解它的好文章。 Does anyone know of any good articles on the subject, with real-world examples to help me understand it better? 有没有人知道关于这个主题的任何好文章,用现实世界的例子来帮助我更好地理解它?

Thanks in advance. 提前致谢。

Check out my answer to a so post, which may help you: 查看我对帖子的回答,这可能对您有所帮助:

Is ASP.net Model View Presenter worth the time? ASP.net Model View Presenter值得花时间吗?

It goes through the differences between MVP and MVC, as often the two are contrasted. 它经历了MVP和MVC之间的差异,因为两者经常形成对比。
Also, there's some helpful links, which show how to easily fit an MVP model to an existing ASP.Net website. 此外,还有一些有用的链接,展示了如何轻松地将MVP模型适用于现有的ASP.Net网站。

HTH HTH

Here's a site devoted to doing mvp on asp.net: http://webformsmvp.com/ MVP as a pattern is kinda obsolete - MVC (which has a great asp.net framework and support) and MVVM (the de facto pattern of WPF) have largely taken over. 这是一个专门在asp.net上做mvp的网站: http ://webformsmvp.com/ MVP作为一种模式有点过时 - MVC(它有一个很棒的asp.net框架和支持)和MVVM(事实上的WPF模式)已经基本上接管了。 In fact, Martin Fowler (MVP's inventor) has documented that the pattern really ought to be split into two patterns, Passive View and Supervising Controller on his site: http://martinfowler.com/eaaDev/ModelViewPresenter.html 实际上,Martin Fowler(MVP的发明者)已经证明,该模式确实应该分为两种模式:Passive View和监督控制器在他的网站上: http//martinfowler.com/eaaDev/ModelViewPresenter.html

If you would like to dig more into this patter, take a look at the Web Client Software Factory 2010 tool. 如果您想更深入地了解这种模式,请查看Web客户端软件工厂2010工具。

This tool is basically used to create composite web applications (modules) but the views are implemented using the MVP pattern. 此工具主要用于创建复合Web应用程序(模块),但视图是使用MVP模式实现的。 If you are going to maintain traditional ASP.Net code or if you want to dig a little bit more in the ASP.Net paradigm, checking the source code of this tool is a great place to start. 如果您要维护传统的ASP.Net代码,或者如果您想在ASP.Net范例中进一步挖掘,那么检查此工具的源代码是一个很好的起点。

This tool requires you to install a couple of extensions to Visual Studio, and after that, you can create a special web project that will implement the MVP for you, it will add contextual menus to Visual Studio to facilitate the tasks 此工具要求您安装Visual Studio的几个扩展,然后,您可以创建一个特殊的Web项目,为您实现MVP,它将向Visual Studio添加上下文菜单以方便任务

Example: 例:

  • Create a new project 创建一个新项目

创建一个项目

  • Add a view with a presenter 使用演示者添加视图

使用演示者添加视图

  • Check the generated files 检查生成的文件

检查生成的文件

  • Check the default generated code: 检查默认生成的代码:

      public partial class MyNewView : Microsoft.Practices.CompositeWeb.Web.UI.Page, IMyNewViewView { private MyNewViewPresenter _presenter; protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this._presenter.OnViewInitialized(); } this._presenter.OnViewLoaded(); } [CreateNew] public MyNewViewPresenter Presenter { get { return this._presenter; } set { if (value == null) throw new ArgumentNullException("value"); this._presenter = value; this._presenter.View = this; } } // TODO: Forward events to the presenter and show state to the user. // For examples of this, see the View-Presenter (with Application Controller) QuickStart: // } public interface IMyNewViewView { } public class MyNewViewPresenter : Presenter<IMyNewViewView> { // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller // The code will not work in the Shell module, as a module controller is not created by default // // private IShellController _controller; // public MyNewViewPresenter([CreateNew] IShellController controller) // { // _controller = controller; // } public override void OnViewLoaded() { // TODO: Implement code that will be executed every time the view loads } public override void OnViewInitialized() { // TODO: Implement code that will be executed the first time the view loads } // TODO: Handle other view events and set state in the view } 

Take a loot at the tool: 抓住工具的战利品:

BTW, If you are going to start a new project, it should be a better idea to use the MVC, if you need to redactor an existing application, you can take as a base the implementation of the MVP in the Web Client Software Factory. 顺便说一句,如果你要开始一个新项目,使用MVC应该是一个更好的主意,如果你需要编写一个现有的应用程序,你可以在Web客户端软件工厂中作为MVP实现的基础。

If you are interested, I think there's a work-through in order to convert an existing application to use the Web Client Software Factory 如果您感兴趣,我认为有一个改进现有应用程序以使用Web客户端软件工厂的工作

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

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