mvvm - Binding Model to ViewModel (WPF) -


i'm making move mvp mvvm, , little confused how best bind viewmodel model. understand how can leverage wpf's data binding infrastructure route events between view , viewmodel using icommand , inotifypropertychanged interface, e.g., view:

public class myview {     public myview()     {         initializecomponent();         datacontext = new myviewmodel();     } } 

and viewmodel:

public class myviewmodel : inotifypropertychanged {     public myviewmodel(){}      public event propertychangedeventhandler propertychanged;      protected virtual void onpropertychanged([callermembername] string propertyname = null)     {         var handler = propertychanged;         if (handler != null) handler(this, new propertychangedeventargs(propertyname));     }      public icommand mycommand ...  } 

this works great!

now, typically mvp i'd have presenter hold reference model via constructor injection, , raise events on model presenter update data in model. tried same approach mvvm, requires viewmodel take model dependency in constructor, seems make things little messy mvvm when using straight out of box without form of ioc (with wpf @ least).

so, 2 questions are:

  1. is injecting model viewmodel right approach, or should implementing inotifypropertychanged interface on model , making use of wpf's binding infrastructure?
  2. to reap benefits of mvvm, should implement ioc , di container, or better still prism?

  1. it "pure" mvvm approach: view must depend on viewmodel. viewmodel bridge between view , model.

    the motivation — definition , responsibilities of model, view , viewmodel , relationships:

    • the model, provides view-independent representation of business entities. design of model optimized logical relationships , operations between business entities, regardless of how data presented in user interface.
    • the view class user interface. displays information user , fires events in response user interactions.
    • the viewmodel class, the bridge between view , model. each view class has corresponding viewmodel class. viewmodel retrieves data from model , manipulates format required view. notifies view if underlying data in model changed, , updates data in model in response ui events view.

    -- implementing model-view-viewmodel pattern, msdn.

    conclusion. injecting model viewmodel seems right approach. also, instead of injecting model can useful inject:

    • the model factory create model — lazy initialization;
    • the service (service facade) retrieve model — lazy loading.
  2. as shown in "mvvm unleashed" book michael brown, following mvvm's potential benefits can leveraged: maintainability, testability, "blendability", portability. @ least, dependency injection (in described case using dependency injection container) allows design follow dependency inversion principle:

    the principle of dependency inversion @ root of many of benefits claimed object-oriented technology. proper application necessary creation of reusable frameworks. critically important construction of code resilient change. and, since abstractions , details isolated each other, code easier maintain.

    -- the dependency inversion principle, robert c. martin, 1996.

    as result, such mvvm's benefits maintainability , testability seem improved when dependency inversion principle followed. dependency injection container tool follow principle.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -