11

私はMVVMパターンにまったく慣れていないので、ご容赦ください。私は、すべてのビューが最上位のインターフェイスとして IView を持つ傾向がある wpf +mvvm + プリズムの実装を見てきました。次に、それぞれのモジュールのビューには、IView インターフェイスを実装する IViewA、IViewB などのビュー固有のインターフェイスがあります。ビューモデルにも IViewModel の最上位インターフェイスがあり、後続のモジュールには IViewmodel から継承する IViewAViewModel 、 IViewBViewModel などがあります。IViewmodel には Iview への参照があり、Iview には IViewModel への参照があります。

namespace xxx.xxx.infrastructure
{
public interface IView
{
  IViewModel ViewModel {get;set;}
}

public interface IViewModel 
{
  IView View {get;set;}
}

public abstract class ViewModelBase : IViewModel, INotifyPropertyChanged
{

   public IView View {get;set;}

   public ViewModelBase(IView view)
   {
     View = view;
     View.ViewModel = this;
   }
   //INotifyPropertyChanged left out
 }
}

namespace xxx.xxx.Modules.Customer
{
   public interface ICustomerDetailsView : IView
   {

   }

   public partial Class CustomerDetailsView : UserControl, ICustomerDetailsView 
   {
       public CustomerDetailsView ()
       {
         InitializeComponent();
       }

       //Is this implementation acceptable?The view is supposed to have zero code in       the code behind.....
        public IViewModel ViewModel
        {
          get
          {
            return (ICustomerDetailsViewViewModel)DataContext; 
          }
          set
          {
             DataContext = value;
          }
        }

    }  

    public interface ICustomerDetailsViewViewModel : IViewModel
    {
       string Message {get;set;}
    }

     public class CustomerDetailsViewViewModel : ViewModelBase,       ICustomerDetailsViewViewModel 
    {
      //Will be injected by unity as i have set up mappings in module initilize.
      public CustomerDetailsViewViewModel(ICustomerDetailsView view)
          :base(view)
      {
      }

       public string Message
       {
          //INotifyPropertyChanged left out for brevity
          get;set;
       }
   }

いくつかの質問を聞きたいんです。

1.)コードビハインドファイルにはコードが含まれていないため、MVVMの違反ではありませんか?

2.)MVVMビューモデルでは、ビューまたはそのコントラクトを心配する必要はありませんか?上記の実装はそれを壊しませんか?

3.) この実装の用途がわかりません。実際、これは MVP に近いものであり、多くのコードが必要です。

4.)これが実装に受け入れられる方法である場合、すべてのモジュールのすべてのビューとビューモデルのインターフェイスが必要ですか?

4

2 に答える 2

18

first a very nice comment from Rachel regarding viewmodel first:

Remember, with MVVM your ViewModels are your application. The View is just a pretty interface that allows users to interact with your ViewModels.

1) IView is a violation of MVVM for me, but codebehind is of course allowed for ui stuff. viewmodel should just have no reference to the view. see 1st comment from Hasith

2) see my blockquote

3) i'm with you - i never use something like that in my projects

4) pls do MVVM the easy way - no coupling, use di, ioc, commanding, behaviors and for me the most important: viewmodel first:)

于 2012-07-13T05:29:45.953 に答える
8

基本的に、インジェクション用のインターフェースのみが必要です。2 つの方法があります。

  • ViewModel をビューに挿入する

これは、ViewModel がビューへの後方参照を持たなくなったことを意味します。つまり、ViewModel の単体テストを行う場合、モック ビューは必要ありません。さらに、ビューのコンストラクターで、挿入されたビューモデルに DataContext を設定するだけで、コードがよりクリーンになります。

  • ViewModel にビューを挿入します。

ワークフロー ロジックをプレゼンテーション層に保持することを回避します。これは、アプリケーション レイヤーがアプリケーション ワークフローを担当し続けることを意味します。このように、ワークフロー ロジックはビューと高度に結合されているため、このための単体テストを作成することはまったく不可能です。

于 2012-07-13T06:09:48.900 に答える