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:
- is injecting
modelviewmodelright approach, or should implementinginotifypropertychangedinterface onmodel, making use of wpf's binding infrastructure? - to reap benefits of mvvm, should implement ioc , di container, or better still prism?
it "pure" mvvm approach:
viewmust depend onviewmodel.viewmodelbridge betweenview,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.
conclusion. injecting
modelviewmodelseems right approach. also, instead of injectingmodelcan useful inject:- the
model factorycreatemodel— lazy initialization; - the
service(service facade) retrievemodel— lazy loading.
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
Post a Comment