MVP 응용 프로그램 (C# Winforms)을 빌드하고 있습니다. 내 초기 버전은 Critique my simple MVP Winforms app ... 이제 복잡성이 커지고 있습니다. 두 개의 별도 텍스트 필드를 두 개의 뷰/발표자 쌍으로 처리하는 코드를 작성했습니다. 간단한 예이지만 동일한 모델을 공유하는 여러 발표자의 세부 정보를 찾아내는 것입니다.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