I have a problem with my MVP structure that is built upon generic presenters, views etc. and I feel I'm violating DRY and I dont know how to get around it.
Example.
public class Presenter<TView, TModel>
where TView : IView
where TModel : Model
{}
So far everything is fine, but I want to have it like this
public class Presenter<TView, TModel>
where TView : IView
开发者_JAVA技巧 where TModel : Model
{}
public class Model<T>
{
public T Value { get;set; }
}
But that won't compile because the where for Model needs a generic parameter. The fix:
public class Presenter<TView, TModel, TModelType>
where TView : IView
where TModel : Model<TModelType>
{}
And it's here I feel i violating dry, take for example
public class MyPresenter : Presenter<IMyView, MyModel, string>
{}
public class MyModel : Model<string>
{}
I feel uncomfortable specifying the string type twice, at the presenter AND at the model, I only wan't to specify that the presenter is using MyModel as Model, I don't care what type of Model (generics). One solution is to remove the generic constraint for the model but then I can't create a generic Model class hierarchy that I want.
Am I thinking wrong about the whole MVP/generics stuff?
In C++, this would be solved with a typedef -- which C# doesn't really have.
public class Model<T>
{
typedef T TModelType; // this doesn't exist in C#
public T Value { get;set; }
}
Then,
public class Presenter<TView, TModel>
where TView : IView
where TModel : Model<TModel::TModelType>
{}
There is a type aliasing that you can do with using
using NewName = any_type<even_generics<with_generics>>
But it's file based -- you can't use it in a class.
I would probably do it like this
- rename
Model<T>
toModelOf<T>
- Make a Model base class for
ModelOf<T>
(ModelOf<T> : Model
) - Use
where TModel : Model
Model itself will be useful to define the interface methods that don't depend on TModelType.
Working with similar solution for my Unity game. Here is my take on MVP architecture. Waiting for your tips to help me improve my code) I still not sure if it's OK that I have two empty interfaces and that my View creates both model and presenter in Awake()
IModel
public interface IModel
{
}
IView
public interface IView
{
public virtual void OnInit() { }
}
IPresenter
public interface IPresenter
{
}
BaseModel
public class BaseModel<TP> : IModel
where TP : IPresenter
{
public TP Presenter { get ; set; }
public BaseModel() { }
}
BaseView
public class BaseView<TP> : MonoBehaviour, IView
where TP : IPresenter
{
public TP Presenter { get; set; }
public virtual void OnInit(){ }
}
BasePresenter
public class BasePresenter<TV, TM> : IPresenter
where TV : IView
where TM : IModel
{
public TV View { get; set; }
public TM Model { get; set; }
public BasePresenter(TV view, TM model)
{
View = view;
Model = model;
View.OnInit();
}
}
BoardModel
public class BoardModel : BaseModel<BoardPresenter>
{
public BoardModel() : base () {
}
}
BoardView
public class BoardView : BaseView<BoardPresenter>
{
public int RowsNumber => meshRenderers.Length;
[SerializeField] private MeshRenderer[] meshRenderers;
public override void OnInit()
{
foreach (var renderer in meshRenderers) {
renderer.enabled = false;
}
}
private void Awake()
{
Presenter = new BoardPresenter(this, new BoardModel());
}
public void OnLineSelected(int index) {
for (int i = 0; i < RowsNumber; i++)
{
meshRenderers[i].enabled = index == i;
}
}
}
BoardPresenter
public class BoardPresenter : BasePresenter<BoardView, BoardModel>
{
public BoardPresenter(BoardView view, BoardModel model) : base(view, model) { }
}
精彩评论