MVP模式View Interface上包含的内容

时间:2012-09-19 23:57:39

标签: winforms mvp

在WinForms应用程序中使用MVP模式我被要求写。请原谅VB.net,因为我被迫使用它:(

成为MVP的新手我已经使用了被动模型实现,其中View&模型,只有Presenter知道

视图是UI的一种表示,哪些功能应该是IVIEW接口的一部分

我是否应该在IView中使用方法/操作/任务,即

 Property QItems As IList(Of QItem)
    Property SelectedQItem As QItem

    Property QueStatus As QueStatus

    Property ReportName As String
    Property ScheduleName As String

    Sub BuildQItems()

    Sub RunQue()

    Sub StopQue()

    Sub CancelCurrent()

    Sub PauseCurrent()

并使调用查看winform中实现的Iview接口

class Winform 
   implements IView


 Private Sub btnCreate_Click(sender As System.Object, e As System.EventArgs) Handles btnCreate.Click Implements IVIEW.Create
    If (_presenter.CreateSchdule()) Then
        MessageBox.Show("Sucessfully Created")
        Close()
    End If
End Sub

End Class

或者我应该保持状态

 Property QItems As IList(Of QItem)
    Property SelectedQItem As QItem

    Property QueStatus As QueStatus

    Property ReportName As String
    Property ScheduleName As String

直接调用Presenter,它是WinForm的一部分而不用担心Iview inreface

_presenter.BuildItems()

_presenter.RunQue()

在使用MVP时,你如何权衡何时进行?

1 个答案:

答案 0 :(得分:2)

如果您指的是被动视图方法,那么您不应该尝试调用演示者或在视图中编写业务逻辑。相反,视图应该创建一个传递自身引用的演示者实例。登录表单示例:

public LoginView() // the Form constructor
{
   m_loginPresenter = new LoginPresenter(this);
}

public void ShowLoginFailedMessage(string message)
{
   lblLoginResult.Text = message;
}

View界面应包含允许演示者将业务对象呈现给视图以及管理UI状态(间接)的属性。例如:

interface ILoginView
{
   event Action AuthenticateUser;
   string Username { get; }
   string Password { get; }
   string LoginResultMessage { set; }
}

演示者会是这样的:

public LoginPresenter(ILoginView view)
{
   m_view = view;
   m_view.AuthenticateUser += new Action(AuthenticateUser);
}

private void AuthenticateUser()
{
   string username = m_view.Username;
   ...
   m_view.ShowLoginResultMessage = "Login failed...";
}

对C#代码感到抱歉,但我暂时没有触及VB.NET。

相关问题