c# - WPF MVVM Design questions -


i'm building wpf-based point-of-sales system practice using mvvm. did not use mvvm framework, instead used relaycommand class in article josh smith http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030

i had written one, without applying mvvm pattern (still using wpf).
database access used entity framework. looks this:

my question is, how achieve using mvvm? firstly, wrote login window using mvvm, , immediate questions are,

  1. how close login window (dispose it) , open mainwindow?
  2. in non-mvvm version, mainwindow contains top section of screenshot, row of buttons.
  3. should further split "button bar" own view , viewmodel, using mainwindow place put together?
  4. if so, how code when button clicked, "button bar"'s viewmodel tell mainwindow's viewmodel load respective views (i used system.windows.controls.page in non-mvvm version) frame? in non-mvvm version, code used simple:

    private void btncheckout_click(object sender, routedeventargs e) {     mainframe.navigated += frame_navigated;     var pgcheckout = new pgcheckout();     mainframe.navigate(pgcheckout); //the frame } 
  5. if not, how load page frame?

if don't use existing framework, have create 1 yourself. imo can benefit because in control of everything, have reinvent lot of things. can describe how it, whether it's right or wrong let votes here decide :)

first thing don't want pollute viewmodel code ui related objects. but, viewmodel view logic , need somehow control presentation (navigation, user messages...). can introduce sort of view service view model controls view related logic, not depend on ui framework. let's like:

public interface iviewservice {    //show message dialog message text    void showmessagedialog(string message);    //show yes/no message dialog message text. retrun true if answer yes     bool askquestion(string message);    //navigate other viewmodel    void navigateto(viewmodel someotherviewmodel);       } 

and have create concrete implementation of such service wpf related logic. how methods implemented , ui framework using (wpf in case).

in viewmodels need somehow object implements iviewservice. can inject using dependency injection, instance service locator or have hard coded static singleton instance. (imo dependency injection way go, complicates things further, need introduce di container , create viewmodel instances using di container).

inside commands can call methods of service.

let’s logincommand of loginviewmodel (relaycommand login button binds to)

private void executelogincommand(object parameter) {       bool loginok = login(.....);       if(loginok)          viewservice.navigateto(new mainwindowviewmodel);       else          viewservice.showmessage("login failed"); } 

most important thing, viewmodel in control of presentation related logic, not know ui framework behind. of "wpf" code in class implements iviewservice. in viewmodels "program interface" not tightly coupled wpf logic, viewmodels "testable" , reuse same viewmodel code on other ui platform.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -