c# - Best Way to Pass Data to new ViewModel when it is initiated -


i working on wpf database application using mvvm-light framework. using simpleioc inject data services, , viewmodels connected view xaml file. however, when opening new view models need pass object view model (lets passing integer of carid can grab actual car database).

right passing using messenger on view, after initializecomponent(). caught view model. work, has set backs.

first, message not sent until after constructor of view model called. lets had thiscar object property in viewmodel, , had secondary properties string called brand, returns thiscar.brand.

    public const string thispersonpropertyname = "thiscar";     private model.car _thiscar;     public model.car thiscar     {                 {             return _thiscar;         }          set         {             if (_thiscar== value)             {                 return;             }              raisepropertychanging(thiscarpropertyname);             _thiscar= value;             raisepropertychanged(thiscarpropertyname);         }     }      public const string brandpropertyname = "brand";     public string brand     {                 {             return thiscar.brand;         }          set         {             if (thiscar.brand == value)             {                 return;             }              raisepropertychanging(brandpropertyname);             thiscar.brand = value;             raisepropertychanged(brandpropertyname);         }     } 

now when constructor gets called, have initialize thiscar in view model constructor so, otherwise null error on thiscar.brand when viewmodel gets created.

thiscar = new carobject(); 

then, after initialized empty object, message called, , pull real car database based on car id.

thiscar = webservice.getcar(carid); 

however, when this, brand property not updated, because nothing calling onpropertychanged brand.

i have 2 work arounds, feel more hacks me. first on manually calling onpropertychanged properties need updated. second remove small properties, , bind directly thiscar property (which means have implement inotifypropertychanged in model).

please let me know if being picky, or if there better way this. way doing works, doesn't sit right me. thank you.

ok, here came with, please let me know if horrible idea.

i created interface called irepository since using dependency injections create view models. here implementation:

public interface irepository {     void getcarid(action<int> callback);     void setcarid(int value); } 

next create class inherits irepository

public class repository : irepository {     private int carid;     public void getcarid(action<int> callback)     {         callback(carid);     }      public void setdictionary(int carid)     {         carid = carid;     } } 

now pass value repository, put parent view model

 r.setcarid(13); 

and recieve in new view model put in view models constructor:

    public view2viewmodel(ichildview i, irepository r)     {         int carid;         r.getcarid((id) => carid = id);         thiscar = getcarfromdatabase(carid);     } 

this seems me fix problem, without breaking mvvm pattern, , since ioc container using creates interface members staic, have data throughout rest of views.

the main problem have feels global variable, best come on it. please let me know if bad idea. thank you.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -