MVP模式-设计问题

发布于 2024-10-09 15:01:54 字数 300 浏览 7 评论 0原文

我们正在尝试在当前项目(asp.net 应用程序)中使用 MVP 模式,但遇到了一些问题。该页面有多个部分,我们对这些独立部分使用用户控件。现在,每个用户控件都有自己的视图和呈现器,并且页面充当基本视图。现在的问题是如何将用户控件所需的数据传递给它?

根据当前的设计,页面呈现器将获取整个页面所需的数据。如何将此数据传递给用户控件呈现器?

我们正在考虑的其他方法是只为用户控件创建视图并使用页面呈现器来处理所有事件。在这种情况下,我们将拥有多个视图接口,由每个用户控制视图实现。但是页面演示者如何与所有不同的视图进行交互呢?

谢谢, 吉本

We are trying to use the MVP pattern in our current project (asp.net app) and have run into some issues. The page has multiple sections and we are using user controls for these independant sections. Now each user control has its own view and presenter and the page acts as the base view. Now the question is how should the data that is needed by user control be passed to it ?

As per current design, the page presenter will get the needed data for the entire page. How can this data be passed to the user control presenter ?

Other approach we are thinking is to instead create only views for user controls and use page presenter to handle all events. In this case, we will hv multiple view interfaces to be implemented by each user control view. But how would the page presenter interact with all the different views ?

Thanks,
jbn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归属感 2024-10-16 15:01:54

为什么用户控件有自己的视图和呈现器?

我建议为页面提供一个视图和演示者,并通过页面视图与用户控件进行交互。如果您需要处理事件或向用户控件传递数据或从用户控件传递数据,您可以将它们公开给页面,并且页面视图可以为演示者包装它。换行可防止演示者直接与用户控件进行通信。

这是一些伪代码:

IFooPageView
{
  string SomeData {get; set;}
  event EventHandler SomeEvent;
}

public class FooPageView : IFooPageView
{
   public event EventHandler SomeEvent;
   public SomeData 
   {
         get { return myUserControl.SomeData;}
         set { myUserControl.SomeData = value;}
   }   

   protected override void OnInitComplete(EventArgs e)
   {
     //handle the user control event
     this.myUserControl.SomeEvent += SomeEvent_EventHandler;
   }

   private void SomeEvent_EventHandler(object sender, EventArgs e)
    {            
        //Raise the user control event to the presenter
        if (SomeEvent!= null)
            SomeEvent(this, EventArgs.Empty);
    }
}

我会阅读 Phil Haack 的 ASP.NET 监督控制器(模型查看演示者)从原理图到单元测试再到代码

Phil 的文章中有很多有关 Web 表单的 MVP 信息。

Why do the user controls have their own views and presenters?

I suggest having a view and presenter for the page and interacting with the user controls via the page view. If you need to handle events or pass data to and from the user controls you can expose them to the page and the page view can wrap it for the presenter. The wrapping prevents the presenter from communicating with the user control directly.

Here is some Pseudocode:

IFooPageView
{
  string SomeData {get; set;}
  event EventHandler SomeEvent;
}

public class FooPageView : IFooPageView
{
   public event EventHandler SomeEvent;
   public SomeData 
   {
         get { return myUserControl.SomeData;}
         set { myUserControl.SomeData = value;}
   }   

   protected override void OnInitComplete(EventArgs e)
   {
     //handle the user control event
     this.myUserControl.SomeEvent += SomeEvent_EventHandler;
   }

   private void SomeEvent_EventHandler(object sender, EventArgs e)
    {            
        //Raise the user control event to the presenter
        if (SomeEvent!= null)
            SomeEvent(this, EventArgs.Empty);
    }
}

I would read Phil Haack's ASP.NET Supervising Controller (Model View Presenter) From Schematic To Unit Tests to Code

There is a lot of good MVP information for web forms in Phil's article.

吹泡泡o 2024-10-16 15:01:54

我能想到的解决方案之一是

1-在顶级演示者处维护一个组成演示者的列表,当您在顶级演示者处获得数据时,您可以通过从列表中选择正确的演示者来分​​发数据。

one of the solution which i can think of

1- Maintain a List of composed presenter at the top level presenter and when you got the data at top level presenter , you can distribute the data by picking the right presenter from the list.

你的心境我的脸 2024-10-16 15:01:54

不确定它是否完全适合 MVP,但您可以从 UC 公开属性(视图)并使用页面中的数据更新它。

Not sure if it exactly fits the MVP but you can expose a property(View) from your UCs and update that with the data in your page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文