Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, you can do this, in as much as the code will work but there is a big potential issue you may run into in the future.</p> <p>One of the strong arguments for using the MVVM pattern is that it makes it easier to write code that can be easily tested.<br> With you're above code you can't test <code>ViewModel1</code> without also having <code>ViewModelLocator</code> and <code>ViewModel2</code>. May be that's not too much of a bad thing in and of itself but you've set a precedent that this type of strong coupling of classes is acceptable. What happens, in the future, when you </p> <p>From a testing perspective you would probably benefit from being able to inject your dependencies. This means passing, to the constructor--typically, the external objects of information you need.</p> <p>This could mean you have a constructor like this:</p> <pre><code>public ViewModel1(string vm2Name) { string name = vm2Name; } </code></pre> <p>that you call like this:</p> <pre><code>var vm1 = new ViewModel1(ViewModelLocator.ViewModel2.name); </code></pre> <p>There are few other issues you may want to consider also.</p> <p>You're also creating a new <code>ViewModelLocator</code> to access one of it's properties. You probably already have an instance of the locator defined at the application level. You're creating more work for yourself (and the processor) if you're newing up additional, unnecessary instances.</p> <p>Do you really need a complete instance of <code>ViewModel2</code> if all you need is the name? Avoid creating and passing more than you need to.</p> <p><strong>Update</strong></p> <p>If you capture the store in the first view/vm then why not pass that (ID &amp;/or Name) to the second VM from the second view? The second VM can then send that to the server with the data captured in the second view.</p> <p>Another approach may be to just use one viewmodel for both views. This may make your whole problem go away.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload