【问题标题】:MVP, should the Presenter or the View be created in the Main()MVP,应该在 Main() 中创建 Presenter 还是 View
【发布时间】:2012-10-16 17:42:49
【问题描述】:

我一直在与 MVP 斗争,特别是如何让程序启动的时间比我承认的要长。目前我在 program.cs 中创建所有类的实例。然后我只需调用 application.Run(userInterface);以下是我现有设置的一部分。

static void Main()
{
    //...

    Status _status = new Status();
    Logger _logger = new Logger(entity, readerWriter, true);
    VerifyRow _verifyRow = new VerifyRow(entity, _logger);
    VerificationOfDataTypes _verification = new VerificationOfDataTypes(entity, _logger, _verifyRow, _status, readerWriter);

    var verify = new CsvFileVerification(entityVerification, _verification, _logger);

    CancellationTokenSource cts = new CancellationTokenSource();
    var source = new CancellationTokenSourceWrapper();

    var presenter = new MainPresenter(userInterface, browser, helper, entity, verify, source);
    Application.Run(userInterface);
}

我有 MVP 设置 atm 的方式,MainView 实现 IMainView。演示者然后将 IMainView 与其他类的负载一起注入到其构造函数中。

    public MainPresenter(IMainForm view, IFileDialog dialog, IMainPresenterHelper helper, IUserInputEntity entity, ICsvFileVerification verify, ICancellationTokenSource source)
    {
        _view = view;
        _dialog = dialog;
        _helper = helper;
        _entity = entity;
        _verify = verify;
        _source = source;

        view.ComposeCollectionOfControls += ComposeCollectionOfControls;
        view.SelectCsvFilePath += SelectCsvFilePath;
        view.SelectErrorLogFilePath += SelectErrorLogFilePath;
        view.DataVerification += DataVerification;
    }

我被告知 MEF 或 IOC 容器会帮助整理这个,但我仍然不确定我应该如何构建它。我有一种直觉,应该首先创建演示者,但是我会在我的 Main() 方法中声明一个随机变量,然后不会使用它。还有如何创建视图?

我之前有一个使用 MEF 的控制台应用程序,但我不知道如何使用 mvp 跳转到 winforms/winforms。

对此的任何指示将不胜感激。

编辑,我尝试了以下但无法使以下工作。它实际上试图创建 2 个视图。我如何引用由

创建的原始视图
"Application.Run(new Form1());"

在 Program.cs 中

以下是我将 Form1 类更改为的内容。

namespace Mini_MVP_MEF
{
    [Export(typeof(IView))]
    public partial class Form1 : Form, IView
    {
        private IPresenter _presenter;

        public Form1()
        {
            InitializeComponent();
            _presenter = Program._myContainer.GetExport<IPresenter>().Value;
        }

        //....

    }
}

也只是尝试在 InitializeComponents() 之后调用以下方法;在Form1中

    private static void PopulateContainer()
    {
        var presenter = new AssemblyCatalog(typeof(Presenter.MVPPresenter).Assembly);
        var model = new AssemblyCatalog(typeof(Model.MVPModel).Assembly);
        var view = new AssemblyCatalog(System.Reflection.Assembly.GetCallingAssembly());
        var aggregateCatalog = new AggregateCatalog(model, presenter, view);
        _myContainer = new CompositionContainer(aggregateCatalog);
    }

但它也不起作用。

【问题讨论】:

    标签: c# inversion-of-control mef mvp


    【解决方案1】:

    没有视图的演示者是没有意义的。它应该在视图将自身传递给演示者的视图中创建。

    例如:

    public class MainView : IMainView
    {
        IMainPresenter _presenter;
        public MainView()
        {
           _presenter = new MainPresenter(this);
        }
    }
    

    顺便说一句,还要确保您的视图具有可注入的演示者,例如。另一个构造函数,您可以在其中注入演示者进行单元测试

    【讨论】:

    • +1。我仍然不确定如何应用 MEF。我找到了codeproject.com/Articles/258681/…,但我还没有机会仔细查看它。
    • 这里不考虑MEF,你需要[ImportingConstructor]属性并让MEF实例化Presenter。
    • 这是不正确的,至少对于 MVP 的 Passive View 实现是不正确的。演示者应该有一个视图,而不是相反。然而,Presenter 将公开对视图的引用,该引用将与 Application.Run(presenter.View);
    • 为什么要为presenter使用接口?它只是空的,防止任何“非法”耦合吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多