私はMVPアプリケーション(C#Winforms)を構築しています。私の最初のバージョンはCritique my simple MVP Winforms appです...今、私は複雑さを増やしています。私は、2つの別々のテキストフィールドを2つのビュー/プレゼンターのペアに扱うコードを作りました。それは簡単な例ですが、同じモデルを共有する複数のプレゼンターの詳細を理解することです。MVP Winforms Appでモデルを共有する
私の質問は、モデルについて、次のとおりです。
私は基本的に何かが変更されたビューを通知するためのモデルが提起したプロパティ変更イベントを使用しています。それは良いアプローチですか?私が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;
}
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;
}
}
TextEditor2Presenter.csは、_model.TheText
の代わりに_model.MoreText
で動作する点を除いて、本質的に同じです。
ITextEditorView.cs
public interface ITextEditorView
{
string TheText { get; set; }
}
ITextEditor2View.cs
public interface ITextEditor2View
{
string MoreText { get; set; }
}
+1大きな質問です。私は私の心の中で同じことをしています。 – Marcel