[MVCSharp]MVC# Overview概述
发布日期:2021-09-08 01:45:14 浏览次数:28 分类:技术文章

本文共 7523 字,大约阅读时间需要 25 分钟。

MVC# Overview概述

Abstract: This article gives an overview of MVC# - a Model-View-Presenter framework for .NET platform. It firstly explains the MVP pattern essentials and then walks through the key features of the MVC# framework which help building true MVP-based solutions.




What is Model-View-Presenter?什么是MVP

3-tier architecture三层架构

One of the most fundamental approaches in software engineering is the Layered architecture. It implies dividing a system into several interacting layers with certain limitations imposed on how layers may interact.s Layered architecture finds its application in various systems for example net protocols (TCP/IP layers), operating systems (three layers: core, drivers, applications) and others.


A particular case of layered architecture is the 3-tier architecture with its variations: Model-View-Controller and Model-View-Presenter. Before considering MVP (and MVC) let us discuss the general 3-tier architecture and its difference to the conventional programming style.


A straightforward (and widely used) approach in designing applications is the 2-tier architecture. According to it an application consists of a presentation layer and a domain layer. Domain layer classes represent the problem domain entities (e.g. customer, order) and are usually bound to some database access facilities. Presentation classes in 2-tier architecture have the following responsibilities:

  • receive user input接受用户输入
  • make necessary calls to the domain tier 必要的调用
  • decide what to show next to the user 确定下面展示给用户什么
  • display output 显示输出

These responsibilities are rather vast and, as a system grows, may result in a bloated presentation layer. Moreover they logically can be divided into two groups: actually presentation logic (code for perceiving input and displaying output) and application logic (communication with the domain tier and application flow decisions). These responsibilities require different programming skills and should better be not mixed in a single module/class. A quite natural solution is to split this too broad presentation layer into two: presentation and application logic:

3-tier architecture is rather abstract. While it declares an existence of three layers, it says nothing about classes in these layers and their interaction. A much more precise form have two 3-tier architecture variations: Model-View-Controller and Model-View-Presenter. Let us proceed to their discussion.

MVC and MVP patterns MVCMVP模式

According to both MVC and MVP the presentation layer consists of view objects, and application logic consists of controller objects (we will use "controller" name instead of "presenter" in MVP). For each view object a corresponding controller exists and vice versa. And although MVC and MVP are based on a common 3-tier principle: views process only presentation needs and controllers handle application logic, these patterns have two major differences:


  1. In MVC controllers receive and process user input, but in MVP views receive user input and then merely delegate processing to the corresponding controllers. That is why MVP pattern better fits modern UI environments (Windows/Web forms) where view classes themselves handle user gestures.

MVC中控制器接收和处理用户输入,在MVP中视图接受用户输入接着委托给相应的控制器处理。这是为什么MVP模式更适合现在的UI环境(Window/Web Forms),视图类自身处理用户的手势。

  1. In MVC controllers affect their views by changing the intermediate presentation model, which the views are subscribed to (by observer pattern). This makes views pure observers without direct access to them. MVP on the other hand violates this "pure observer" rule by providing a direct link from a controller to its view. This makes MVP more handy as compared to MVC.


备注:PresentationModel代表了presentation(可视化界面,或者成为视图)的状态和行为,并且是完全独立于界面中使用的GUI控件的…… Presentation Model把视图(View)的状态和行为抽取到model类中,仍然属于presentation层。 Presentation Model要与domain层协同,为视图提供接口,来最小化在view中需要做的决定。视图(view)或者把状态存储到Presentation Model中,或者频繁的与Presentation Model同步状态。

The said differences make the MVP pattern more attractive than MVC from the developer's point of view. And indeed MVP was designed to be an evolution of MVC and to improve the latter. That is why we often refer to MVP as "sharp MVC" and therefore the name our MVP framework is MVC#.

所述的不同从观察者的角度使MVPMVC更具吸引力。事实上MVP被设计来发展MVC。这使我们为啥称MVPSharp MVC 的原因,也是我们的MVP框架称为MVC# 的原因。

Why using MVC#?

Now that we are convinced in the usefulness of the MVP pattern we may start using it in our applications. However it may be not as easy. Maintaining an additional application logic layer may require considerable efforts. For example a developer needs to take care of linking between all views and appropriate controllers,

Fortunately MVC# automates and takes on itself much of the work concerned with MVP usage. Thus it simplifies and speeds up the development of MVP applications. Below is the list of MVC# framework features:

1. Views and controllers get connected automatically

Developers do not have to care about associating views with their controllers. MVC# framework automatically establishes links between views and corresponding controllers:

开发者不必关心视图和控制器的关联。MVC# 框架自动实现视图和相应的控制器的连接。

publicclass OrderDetailsView


    privatevoid processOrderButton_Click(object sender, EventArgs e)


        // No code needed to establish a link to the controller

        (Controller as OrderDetailsController).ProcessOrder();


2. Multiple GUI platforms supported

MVC# allows targeting different GUI platforms (Windows, Web, Silverlight, etc.) Thus the same application can be used with quite different presentation layers - one for Windows, the other for Silverlight or Web environment, etc.:

3. Platform-independent navigation to views 平台无关导航到视图。

To make application logic fully independent of the presentation layer, MVC# provides a platform-independent way of navigating to views. Instead of  activating a Windows form or redirecting to a Web page a developer should simply call a uniform Navigator.Navigate(...) method:

为了使应用逻辑完全独立于表现层,MVC# 提供了一个平台无关的导航到视图的方法。无论是激活Windows窗体还是导航到WebPage只要通过调用Navigator.Navigate(...)方法。

publicclass OrderDetailsController


    publicvoid ProcessOrder()


        // No Response.Redirect(...) or Form.Show() calls



4. Tasks concept任务的概念

Another useful feature of MVC# framework, although not directly related to the MVP pattern, is the Task concept. A task unites several views with their controllers in fulfilling some job. For example a ticket booking task may consist of two views: one to choose a ticket, the other - to do the payment. In MVC# all controllers within a task are given a link to the task object. Generally a task can be expressed as a workflow or a state machine.




MVC# framework frees developers from much of extra work required in construction of Model-View-Presenter applications. It allows creating flexible MVP-based application with almost no extra cost. For more information on MVC# including the  of using it see the .


转载地址:https://blog.csdn.net/weixin_34310369/article/details/85621312 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

下一篇:ubuntu下安装vmTools, 和共享文件



[***.116.15.85]2023年05月31日 22时41分20秒


-- 愿君每日到此一游!