c# - How to ensure injected property updates its reference in IoC in order to have all depended ViewModels have same property instanceup to date -


it possible inject interface via constructor:

   private readonly idataservice _dataservice;      public mainviewmodel(idataservice dataservice)     {         _dataservice = dataservice;     } 

above injection proper if service created in ioc container , never changes.

another way inject via property (property injection)

   public idialogservice dialogservice     {                 {             return simpleioc.default.getinstance<idialogservice>();         }         } 

such solution useful if instance of idialogservice may changed during application lifetime.

then change data:

  dialogservice.somedata = updateddata 

the first way injection done easy test.

i can mock interface , inject via constructor.

i know practise ensure above second way testable.

i ensure depended viewmodels using same idialogservice (being changed here in mainviewmodel) has date same instance.

your example doesn't show property injection. example shows container being used outside of composition root bad idea. should construct object graph in single place, afore mentioned composition root, , should not allow container used outside of location.

you should prefer constructor injection on property injection, , see this excellent answer

property injection using container set property value (in composition root) not value container every time property called.

if set initial value of property variable passed in constructor (which can done preferred method of constructor injection) allow set via property normal, easy test (just set call again , check instance set) , doesn't rely on container outside of composition root.

edit do, based on response:

public idialogservice dialogservice {         {         return dialogserviceprovider.provideservice();     }        set     {         dialogserviceprovider.setnewservice(value);     } }  public class dialogserviceprovider() {      public dialogserviceprovider(idialogservice service)      {           //save dialog service      }       public provideservice(){ //return }       public setnewservice(idialogservice newservice){//overwrite existing reference} } 

now long same instance of dialogserviceprovider injected each view model, , changes idialogservice of them make seen others when next call provideservice


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -