2009-06-08 4 views
0

MVP를 이해하려면 귀하의 도움이 필요합니다. 인터페이스 (IProductEditorView)를 사용했습니다. 아래를 보면 프리젠 테이션 레이어를 볼 수 있습니다 :모델 발표자 인터페이스 질문보기?

using System; 
using System.Collections.Generic; 
using System.Text; 
using MVPProject.BusinessLayer; 

namespace MVPProject.PresentationLayer 
{ 
    public interface IProductEditorView 
    { 
     int ProductID { get;} 
     string ProductDescription { get; } 

     event EventHandler<EventArgs> Save; 
    } 

    public class ProductEditorPresenter 
    { 
     private IProductEditorView mView; 

     public ProductEditorPresenter(IProductEditorView view) 
     { 
      this.mView = view; 
      this.Initialize(); 
     } 

     private void Initialize() 
     { 
      this.mView.Save += new EventHandler<EventArgs>(mView_Save); 
     } 

     private void mView_Save(object sender, EventArgs e) 
     { 
      Product product; 
      try 
      { 
       product = new Product(); 
       product.Description = mView.ProductDescription; 
       product.ID = mView.ProductID; 
       product.Save(); 
      } 
      catch (Exception ex) 
      { 
       System.Diagnostics.Debug.WriteLine(ex.Message); 
       throw; 
      } 
     } 
    } 

} 

ProductListPresenter(IProductEditorView view)하지만 그들은 내게 (this)을 추가하기 때문에 내 머리가 섞여있다. 이유를 모르겠다 "this.mPresenter = new ProductListPresenter(this);"?

private ProductListPresenter mPresenter; 
    protected override void OnInit(EventArgs e) 
    { 
     base.OnInit(e); 
     this.mPresenter = new ProductListPresenter(this); 
    } 
+1

아마 asp.net-mvc 태그를 제거해야합니다. – DSO

답변

0

코드의 두 번째 블록은 뒤에 asp.net 양식 코드의 일부입니다 가정하면, 그때는 asp.net 양식이 IProductEditorView 인터페이스를 구현함으로써 당신이보기를 (이) 통과되는 것을 상상할 수 발표자.

0

코드의 두 번째 부분이보기에 있다고 가정하면 MVP 패턴 구현에 문제가 있습니다.

디자인에서 발표자와보기는 모두 서로에 대해 알고 있습니다 (Presenter는 해당 생성자에서보기를 허용하고보기는 OnInit에서 발표자를 설정합니다).

뷰와 발표자를 분리하기 위해 MVP를 사용하지만이 디자인이 밀접하게 결합되어 있기 때문에 이것은 문제가됩니다. 발표자는 자신의 견해에 대해 알 필요가 없으므로 발표자 생성자에서 IProductEditorView 매개 변수를 제거 할 수 있습니다.

그런 다음 여기에 방법을 저장을 변경해야합니다

private btnSaveProduct_Click(object sender, EventArgs e) 
{ 
    Product product; 
    product.Description = txtDescription.Text; 
    // Set other properties or the product 
    this.mPresenter.Save(product); 
} 
:
private void Save(Product product) 
{ 
    try 
    { 
     product.Save(); 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine(ex.Message); 
     throw; 
    } 
} 

그리고 당신은, 제품을 저장할 준비를하고 발표자로 전달해야보기에

보기의 OnInit은 동일하게 유지됩니다.

+0

감사합니다. 괜찮습니다! Teşekkürler Dostum. Benim meesengerım : [email protected] eklersen tanışırız ... – Penguen

0

MVP를 수행 한 방법은 표현 자에게 인수로 뷰를 가져 오는 Initialize라는 단일 메서드가 있어야합니다. MVP를 생각하는 한 가지 방법은 발표자가보기 인터페이스를 통해보기를 조작하는 것입니다. 프리 네 터가 제품을 저장하고 페이지의 코드 숨김 코드에이 코드를 직접 저장하지 않도록하려는 저장 버튼의 OnClick 이벤트가 클릭 된 경우를 예로 들어 보겠습니다. 다음과 같이 코드를 작성할 수 있습니다.

public class ProductEditorPresenter 
{ 
    public void Initialize(IProductEditorView view, IProductBuilder productBuilder) 
    { 
     view.SaveProduct += delegate 
          { 
           var product = productBuilder.Create(view.ProductId, view.ProductDescription); 
           product.Save(); 
          } 
    } 
} 

여기서 productBuilder는 제품 생성을 처리합니다. 인터페이스/계약 기반 프로그래밍을 시도하고 수업에서 직접 구체적인 객체를 만들지 않으려 고합니다. 또한 Jeremy Miller는이 패턴을 더 자세히 설명하는 데 유용한 프리젠 테이션 패턴에 대한 위키를 만들었습니다. 이것은 볼 수 있습니다 here

관련 문제