c# - MVP,应该在 Main() 中创建 Presenter 还是 View

标签 c# inversion-of-control mef mvp

我一直在与 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 的方式是,MainView 实现 IMainView。然后,演示者将 IMainView 以及其他类的负载注入(inject)到其构造函数中。

    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() 方法中声明一个随机变量,然后该变量将不会被使用。那么 View 是如何创建的呢?

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

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

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

创建的原始 View
"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);
    }

但它也不起作用。

最佳答案

没有观点的演示者是没有意义的。它应该在 View 内部创建, View 将自身传递给演示者。

例如:

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

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

关于c# - MVP,应该在 Main() 中创建 Presenter 还是 View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12920445/

相关文章:

c# - 动态呈现控件,从字符串/XML 文件确定类型?

c# - 在 C# 中使用 Regex 提取 Form 标记中的操作属性?

c# - .NET 图表控件 : How to use a LineAnnotation?

dependency-injection - 与接口(interface)、工厂和控制反转混淆

c# - Prism v4,MEF WPF 数据绑定(bind)

c# - MEF 2,带有实现类的通用导入

c# - 通过 TCP 发送和接收 XML 数据

c# - 翻译 ninject ISecureDataFormat 绑定(bind)到 Autofac

asp.net-mvc-3 - 如何将 HttpContext 传递给 MVC3 应用程序中的依赖项初始化

c# - 我如何告诉 MEF 该接口(interface)需要哪种类型?