Kei*_*h G 5 c# model-view-controller mvp design-patterns winforms
我正在建立一个MVP应用程序(C#Winforms).我的初始版本是在Critique我简单的MVP Winforms应用程序 ......现在我增加了复杂性.我已经打破了代码来处理两个单独的文本字段到两个视图/演示者对.这是一个简单的例子,但它是为了解决共享相同模型的多个演示者的细节.
我的问题是关于模型:
我基本上使用模型引发的属性更改事件来通知视图已发生变化.这是一个好方法吗?如果它达到100或1000个属性的程度怎么办?那时它仍然实用吗?
是否使用NoteModel _model = NoteModel.Instance 正确的方法在每个演示者中实例化模型 ?请注意,我确实希望确保所有演示者共享相同的数据.
如果有更好的方法,我愿意接受建议......
我的代码看起来像这样:
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)