2012-10-16 86 views
2

我一直在與MVP作鬥爭,具體如何讓程序開始時間比我承認的要長。目前,我在program.cs中創建了所有類的實例。然後我只是調用application.Run(userInterface);以下是我現有設置的一部分。MVP,如果演示者或視圖創建在主要()

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

但它不工作。

回答

2

沒有意見的主持人沒有意義。它應該在視圖內將視圖傳遞給演示者的地方創建。

如:

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

BTW,還要確保你的觀點有注射的主持人,如。另一個構造函數,您可以爲單元測試注入演示者

+0

+1。我仍然不確定MEF如何應用。我發現http://www.codeproject.com/Articles/258681/Windows-Forms-Modular-App-using-MEF,但我沒有機會通過它進行適當的觀察。 –

+2

這不考慮MEF,您需要[ImportingConstructor]屬性並讓MEF實例化Presenter。 –

+1

這是不正確的,至少對於MVP的被動視圖實現。演示者應該有一個視圖,而不是相反。 Presenter會公開對視圖的引用,然而,它將與Application.Run(presenter.View)一起使用; –

相關問題