MVP中的会话管理 - 简化示例

时间:2012-01-20 08:36:36

标签: c# asp.net .net webforms mvp

我在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>

&nbsp;<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 session response request

1 个答案:

答案 0 :(得分:0)

我通常使用下一种方式:

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。 希望它有所帮助...