Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is what I do:</p> <p>First, I define theses interfaces:</p> <pre><code>public interface IView&lt;TPresenter&gt; { TPresenter Presenter { get; set; } } public interface IPresenter&lt;TView, TPresenter&gt; where TView : IView&lt;TPresenter&gt; where TPresenter : IPresenter&lt;TView, TPresenter&gt; { TView View { get; set; } } </code></pre> <p>Then this abstract presenter class: </p> <pre><code>public abstract class AbstractPresenter&lt;TView, TPresenter&gt; : IPresenter&lt;TView, TPresenter&gt; where TView : IView&lt;TPresenter&gt; where TPresenter : class, IPresenter&lt;TView, TPresenter&gt; { protected TView view; public TView View { get { return this.view; } set { this.view = value; this.view.Presenter = this as TPresenter; } } } </code></pre> <p>The view is injected via a property, instead of the constructor, to allow the bi-directional affection in the setter. Notice that a safe cast is needed...</p> <p>Then, my concrete presenter is something like :</p> <pre><code>public class MyPresenter : AbstractPresenter&lt;IMyView, MyPresenter&gt; { //... } </code></pre> <p>Where <code>IMyView</code> implements <code>IView</code>. A concrete view type must exists (e.g. <code>MyView</code>), but it's the container that resolves it:</p> <ol> <li>I register <code>MyPresenter</code> type as itself in the container, with a transient behavior.</li> <li>I register <code>MyView</code> as an <code>IMyView</code> in the container with a transient behavior.</li> <li>I then asks for a <code>MyPresenter</code> to the container.</li> <li>Container instanciate a MyView</li> <li>It instanciates a <code>MyPresenter</code></li> <li>It inject the view into the presenter through the <code>AbstractPresenter.View</code> property.</li> <li>The setter code completes the bi-directional association</li> <li>The container returns the couple Presenter/View</li> </ol> <p><strong>It allows you to inject other dependencies (services, repos) into both your view and your presenter.</strong> But in the scenario you described, I recommend you to inject services and caches into the <strong>presenter</strong>, instead of the view.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload