asp.net 中的 MVP,在哪里编程用现有数据填充 UI?

发布于 2024-11-26 21:56:01 字数 391 浏览 2 评论 0原文

我在一个新客户那里实施了 MVP 模式。

现在我想用它,但我在他们的代码中找不到的是: 我打开一个 asp 表单,查询字符串中有一个 id。我获得了具有该 id 的数据对象,现在我有了一个想要在 asp.net 表单的文本框中显示的对象。

我有这个:

我创建了一个演示者,

演示者有一个构造函数,它接受一个I​​xxxView,

aspx页面

在aspx中实现IxxxView,我在aspx的加载中引用了演示者

,我用(这个)实例化演示者

我通过将对象的 id 发送给演示者,演示者获取数据...

(到目前为止一切顺利?)

然后,我在演示者中有一个对象,视图中有文本框。

我应该知道什么?

i'm at a new client where they implemented the mvp pattern.

now i want to use it to, but what i can't find in their code is this:
i open a asp form with an id in the querystring. i get the dataobject with that id, and now i've got an object which i want to show in the textboxes in the asp.net form.

i have this:

i created a presenter

the presenter has a constructor which takes a IxxxView

the aspx page implements the IxxxView

in the aspx i have a reference to the presenter

in the load of the aspx, i instantiate the presenter with (this)

i pass the id of the object to the presenter, the presenter gets the data...

(so far so good?)

and then, i have an object in the presenter, and textboxes in the view.

what should i do know?

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

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

发布评论

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

评论(1

我不咬妳我踢妳 2024-12-03 21:56:01

在相当典型的 MVP 实现中,我希望 IView 接口将指定事件。当 Presenter 获取对 IView 的引用时(在您的情况下,该引用位于 Presenter 的构造函数中),应该为它想要处理的任何事件附加事件处理程序。通常,事件之一是向演示者发出信号,表明数据应放入视图中,或者放入视图模型中以供视图绑定。有时,事件会表明演示者应该采取某种操作,例如更新模型或启动流程。

在 WebFormsMvp 中,在演示者将数据加载到视图的视图模型中(通过设置 View.Model 属性)后,视图可以使用绑定语法绑定到视图模型。例如,您的文本框控件可能看起来像这样:

<asp:textbox runat="server" DataSource="<%# Model.Data %>" />

如果您没有使用 WebFormsMvp,那么您只想确保 Presenter 能够将数据传递到视图中。您可以通过向用户控件实现的 IView 添加属性来完成此操作。当调用属性的设置器时,需要将值传递到适当的控件中。同样,当调用该属性的 getter 时,它应该返回该控件的值。

例如,您的用户控件可以包含如下所示的属性实现:

string IMyView.ViewData
{
    get { return TextBox1.Text; }
    set { TextBox1.Text = value; }
}

但是,如果您想变得更奇特,您也可以使用 DataSource 控件。要记住的主要事情是,如果您采用 MVP,则希望视图部分尽可能简单,因为视图比演示者更难测试。因此,视图应该尽可能少地执行操作——设置值、获取值和触发事件,以及很少的其他操作。动态视图行为(如隐藏或显示元素、突出显示或不突出显示等)应由 Presenter 使用 IView 接口的属性或通过 Presenter 和 View 共享的 ViewModel 来控制。

就我个人而言,我更喜欢并推荐使用 WebFormsMvp。

In a fairly typical MVP implementation, I would expect that the IView interface will specify events. The Presenter, when it gets the reference to the IView (in your case, that would be in the presenter's constructor), should attach event handlers for any event it wants to handle. Very often, one of the events will be to signal the presenter that the data should be put into the view, or put into a view model for the view to bind to. Sometimes, the event will signal that the presenter should take some kind of action, like updating the model, or starting a process.

In WebFormsMvp, after your presenter has loaded the data into the view's viewmodel (by setting the View.Model property), the view can bind to the viewmodel by use of binding syntax. For example, your textbox control might look something like this:

<asp:textbox runat="server" DataSource="<%# Model.Data %>" />

If you are not using WebFormsMvp, then you just want to make sure the Presenter has a way to pass the data into the view. You could do this by adding a property to the IView that the user control implements. When the setter for the property is called, it will need to pass the value into the appropriate control. Likewise, when the getter for that property is called, it should return the value of that control.

For example, your user control could include a property implementation like this:

string IMyView.ViewData
{
    get { return TextBox1.Text; }
    set { TextBox1.Text = value; }
}

However, if you want to get fancy, you could work with DataSource controls, as well. The main thing to remember is that if you are adopting MVP, you want to keep the View part as simple as possible, because views are more difficult to test than presenters. So the View should do as little as possible -- set values, get values, and fire events, and very little else. Dynamic View behavior like hiding or showing elements, highlighting or not highlighting, etc., should be controlled by the Presenter using either properties of the IView interface, or through the ViewModel that the Presenter and View share.

Personally, I prefer and recommend using WebFormsMvp.

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