c# - Is it good practice to make a Model static in WPF (MVVM)? -


let's some, not all, models in application static , defined members of baseviewmodel such multiple viewmodels (and extension, views) can access exact same data hitting have inherited. here basic layout 2 derived classes can access same model:

public class employeemodel {     public string name;     public int id; }  public class baseviewmodel {     private static employeemodel employeemodel = new employee model();     public employeemodel emodel      {          { return employeemodel; }          set { employeemodel = value; }      }      public baseviewmodel() {} }  public class employeeviewmodel : baseviewmodel {     public employeeviewmodel()      {         base.emodel.name = "";     } }  public class homeviewmodel : baseviewmodel {     public employeeviewmodel()     {         base.emodel.name = "";     } } 

in end, got job done same data showing in multiple views without issue. however, not mean there isn't more appropriate way of unaware. new wpf, feel compelled ask, "is making model static practice mvvm pattern?" in addition, can implementation optimized, , if so, how?

i isn't practice. should favour composition on inheritance. if wish of views share common piece of ui shows employee information (presumably logged in employee), you're talking view composition.

if you're using mvvm should use mvvm framework. caliburn.micro makes view composition incredibly easy. in case, have example headerviewmodel corresponding headerview, , headerviewmodel take employee model dependency (for example injected constructor).

the other view models take headerviewmodel (or factory) dependency through constructors.

if you're referring displaying same model in different ways, pass model dependency via constructor view model (assuming dependency required), , have view model either expose model directly (violating lod) or delegate calls model (violating dry principle).


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -