wpf - Should MVVM Locator invoke any registered ViewModel Funcitons? -
wpf - Should MVVM Locator invoke any registered ViewModel Funcitons? -
assume there wpf application uses mvvm lite toolkit.
good illustration of toolkit locator. great contains simpleioc enables register services , interface driven.
sometimes locator constructor can grows. unfortunatelly besides registering interfaces contains logic as.:
if(simpleioc.default.getinstance<mainviewmodel>().loadproject()) { var project = simpleioc.default.getinstance<mainviewmodel>().loadedproject simpleioc.default.register<configservice>(new service(project)) } it illustration - if need more logic during locator constructor. maybe services architecture wrongly created since they not independent or maybe in such cases should resign locator usage - lost di.
another things in few viewmodels there reference locator.getinstance found not practise since should injected via contructor enable testability.
another aspect using correctly avalondock.
avalondock great anchorable command enable prepare apps similar such apps visual studio anchorable, dockable panes.
this panes actualy viewmodels bound avalondock via property.
mainviewmodel has property tools = new tools[] { viewmodel1, viewmodel2}
but each viewmodel registered in locator.
therefore using (violate dry) them in mainviewmodel as
property getter: locator.getinstance()
which in sentiment bad illustration - not safe. if service required avalon tool viewmodel1 not registered yet invoked via getter during mainviewmodel instantiation.
it started mismatch. have practise?
i found many examples such workspaces (mainviewmodel) nobody used @ same time locator found useful.
i maintain locator because consider thing enables mme have project testable dependency injection , interface driven.
any ideas suggestions? thankful.
i'd avoid this. rightly point out, there lot more going on here registering types.
instead, i'd pass configservice instance viewmodel in constructor. long register first, can ioc config service , view model.
simpleioc.register<iviewmodel>(()=>{return new viewmodel(simpleioc.getinstance<iconfigservice>())}); ... that's memory might not accurate show custom constructor thought simpleioc.
in view model constructor can phone call service .register method passing parameter.
that keeps 'knowledge' in view model , locator stays focused on doing should.
wpf mvvm mvvm-light avalondock testability
Comments
Post a Comment