C # Fun with Generics - Interdependencies

As an experiment, I'm trying to write a general MVP structure.

I started with:

public interface IPresenter<TView> where TView: IView<IPresenter<...
{
    TView View { get; set;}
}

public interface IView<TPresenter> where TPresenter:IPresenter<IView<...
{
    TPresenter Presenter { get; set; }
}

It is obvious that this is impossible, because the types TViewand TPresentercan not be resolved. You write Type<Type<...forever. So, my next attempt looked like this:

public interface IView<T> where T:IPresenter
{
    ...
}

public interface IView:IView<IPresenter>
{

}

public interface IPresenter<TView> where TView: IView
{
    ...
}

public interface IPresenter: IPresenter<IView>
{
    ...
}

It actually compiles, and you can even inherit from interfaces such as this:

public class MyView : IView, IView<MyPresenter>
{
    ...
}

public class MyPresenter : IPresenter, IPresenter<MyView>
{
    ...
}

, , . , . , . , . ? ?

+3
3

, , , IView IPresenter - MVP , ( - ), ( ).

+2

, , .

public interface IPresenter<P, V> 
    where P : IPresenter<P, V>
    where V : IView<P, V>
{
}

public interface IView<P, V> 
    where P : IPresenter<P, V>
    where V : IView<P, V>
{
}

public class MyView : IView<MyPresenter, MyView>
{
}

public class MyPresenter : IPresenter<MyPresenter, MyView>
{
}
+3

Your view should match the type of your model, e.g. TModel. The host should take the look of the TV. I do not think there should be a TPresenter.

0
source

Source: https://habr.com/ru/post/1737740/


All Articles