2

我一直在与 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(); 之后调用以下方法;在表格 1

    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);
    }

但它也不起作用。

4

1 回答 1

2

A presenter without a view does not make sense. It should be created inside the view where the view will pass itself to the presenter.

eg:

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

BTW, also make sure your view has injectable presenter, eg. another constructor where you can inject the presenter for unit tests

于 2012-10-16T17:50:31.063 回答