在MVP Winforms App中共享模型

Kei*_*h G 5 c# model-view-controller mvp design-patterns winforms

我正在建立一个MVP应用程序(C#Winforms).我的初始版本是在Critique我简单的MVP Winforms应用程序 ......现在我增加了复杂性.我已经打破了代码来处理两个单独的文本字段到两个视图/演示者对.这是一个简单的例子,但它是为了解决共享相同模型的多个演示者的细节.

我的问题是关于模型:

  1. 我基本上使用模型引发的属性更改事件来通知视图已发生变化.这是一个好方法吗?如果它达到100或1000个属性的程度怎么办?那时它仍然实用吗?

  2. 是否使用NoteModel _model = NoteModel.Instance   正确的方法在每个演示者中实例化模型   ?请注意,我确实希望确保所有演示者共享相同的数据.

  3. 如果有更好的方法,我愿意接受建议......

我的代码看起来像这样:

NoteModel.cs

public class NoteModel : INotifyPropertyChanged
{
    private static NoteModel _instance = null;

    public static NoteModel Instance
    {
        get { return _instance; }
    }

    static NoteModel()
    {
        _instance = new NoteModel();
    }

    private NoteModel()
    {
        Initialize();
    }

    public string Filename { get; set; }
    public bool IsDirty { get; set; }
    public readonly string DefaultName = "Untitled.txt";

    string _sText;
    public string TheText
    {
        get { return _sText; }
        set
        {
            _sText = value;
            PropertyHasChanged("TheText");
        }
    }

    string _sMoreText;
    public string MoreText
    {
        get { return _sMoreText; }
        set
        {
            _sMoreText = value;
            PropertyHasChanged("MoreText");
        }
    }

    public void Initialize()
    {
        Filename = DefaultName;
        TheText = String.Empty;
        MoreText = String.Empty;
        IsDirty = false;
    }

    private void PropertyHasChanged(string sPropName)
    {
        IsDirty = true;

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(sPropName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
Run Code Online (Sandbox Code Playgroud)

TextEditorPresenter.cs

public class TextEditorPresenter
{
    ITextEditorView _view;
    NoteModel _model = NoteModel.Instance;

    public TextEditorPresenter(ITextEditorView view)//, NoteModel model)
    {
        //_model = model;
        _view = view;
        _model.PropertyChanged += new PropertyChangedEventHandler(model_PropertyChanged);
    }

    void model_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "TheText")
            _view.TheText = _model.TheText;
    }

    public void TextModified()
    {
        _model.TheText = _view.TheText;
    }

    public void ClearView()
    {
        _view.TheText = String.Empty;
    }
}
Run Code Online (Sandbox Code Playgroud)

TextEditor2Presenter.cs基本上是相同的,除了它操作_model.MoreText而不是_model.TheText.

ITextEditorView.cs

public interface ITextEditorView
{
    string TheText { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

ITextEditor2View.cs

public interface ITextEditor2View
{
    string MoreText { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

tst*_*ter 3

  1. 这个办法很好。但是,如果您正在考虑拥有数百个(甚至数千个!)属性,那么我认为您可能拥有上帝级(反模式)。拥有 100 个属性的好类并不多。相反,请考虑将模型分解为更小的类。此外,您不需要为每个属性举办单独的活动。如果模型发生了任何更改,您可以触发一个事件(可能包括描述更改的信息),并且视图可以从那里处理它。
  2. 我会避免使用单例模式,除非您确实确定要应用它。相反,更改所有视图的构造函数以接受模型的实例。