MVP中的会话管理-简化示例
本文关键字:管理 会话 MVP | 更新日期: 2023-09-27 18:20:48
我在asp页面中有一个文本框,它是一个具体的视图。用户输入一个2000个字符长的字符串。当按下保存按钮时,该值将保存到数据库中。保存页面后,我重定向到另一个页面,输入的值显示为标签。我曾经在会话中存储文本框的值。当我在MVP中使用它时,我该如何处理它?
1) 如何在TextInputPage中使用MVP概念设置会话的值?
2) 如何在使用MVP概念阅读会话后在标签中显示结果?
注意:在向会话添加值之前,需要进行一些处理(文本附加)。
如果你能用代码示例回答,而不是指向另一篇文章,那就太好了。这是新来的人学习的最简单的例子(我认为:-)。
using System;
namespace ViewInterfaces
{
public interface ITextView
{
string InputtedText { get; }
event EventHandler ButtonClickedEvent;
}
}
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
<asp:Button ID="btnGoToResult" runat="server" Text="Go To Result"
onclick="btnGoToResult_Click" />
</div>
</form>
using System;
using ViewInterfaces;
using Presenter;
using Model;
public partial class TextInputPage : System.Web.UI.Page, ITextView
{
public event EventHandler ButtonClickedEvent;
private TextPresenter listPresenter;
protected void Page_Load(object sender, EventArgs e)
{
TextModel modelController = new TextModel();
listPresenter = new TextPresenter(this, modelController);
this.listPresenter.OnViewLoaded();
}
public string InputtedText
{
get
{
return txtInput.Text;
}
}
protected void btnGoToResult_Click(object sender, EventArgs e)
{
if (ButtonClickedEvent != null)
{
//Riase the event
ButtonClickedEvent(this, e);
}
//Session does not use any MVP now.
if (txtInput.Text.Length > 0 && txtInput.Text.Length <= 100)
{
Session["TextInput"] = "1-100 "+txtInput.Text;
}
else if (txtInput.Text.Length > 100 && txtInput.Text.Length <= 1000)
{
Session["TextInput"] = "101-1000" + txtInput.Text;
}
else
{
Session["TextInput"] = "1001 - 2000" + txtInput.Text;
}
//Redircting currenlty does not use any MVP concept
Response.Redirect("/SessionTestWebSite/ResultOutputPage.aspx");
}
}
<form id="form1" runat="server">
<div>
<asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label>
</div>
</form>
using System;
public partial class ResultOutputPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Does not use MVP now
lblResult.Text = (string) Session["TextInput"];
}
}
using System;
using ViewInterfaces;
using Model;
namespace Presenter
{
public class TextPresenter
{
private ITextView viewObj;
private TextModel contactsModelController;
public TextPresenter(ITextView view, TextModel controller)
{
viewObj = view;
contactsModelController = controller;
}
//Presenter Method
public virtual void OnViewLoaded()
{
//Event subscription
viewObj.ButtonClickedEvent += new EventHandler(DetailView_EditClicked);
}
void DetailView_EditClicked(object sender, EventArgs e)
{
//Calling Model's Functionality
contactsModelController.StoreText(viewObj.InputtedText);
}
}
}
namespace Model
{
public class TextModel
{
public void StoreText(string inputString)
{
//Store to database
}
}
}
参考文献:mvp会话响应请求
我通常使用下一种方式:
public interface ITextView
{
string InputtedText { get; }
string SessionTextEntry { get; set; }
void RedirectToTestPage();
event EventHandler ButtonClickedEvent;
}
视图实现:
public partial class TextInputPage : System.Web.UI.Page, ITextView
{
...
public string SessionTextEntry
{
get { return (string)Session["TextInput"]; }
set { Session["TextInput"] = value; }
}
public void RedirectToTestPage()
{
Response.Redirect("/SessionTestWebSite/ResultOutputPage.aspx");
}
...
}
演示者:
public class TextPresenter
{
...
void DetailView_EditClicked(object sender, EventArgs e)
{
//Calling Model's Functionality
contactsModelController.StoreText(viewObj.InputtedText);
// Save to session and redirect
viewObj.SessionTextEntry = GetTextForSession(viewObj.InputtedText);
viewObj.RedirectToTestPage();
}
// data processing logic
private string GetTextForSession(string inputtedText)
{
if (inputtedText.Length > 0 && inputtedText.Length <= 100)
return "1-100 " + inputtedText;
if (inputtedText.Length > 100 && inputtedText.Length <= 1000)
return "101-1000" + inputtedText;
return "1001 - 2000" + inputtedText;
}
...
}
在这种情况下,只使用ITextView.SessionTextEntry的setter,因此可以删除getter。希望它能帮助。。。