title: MVVM Handling Window Management using Factory Approach
body: <p>I delegate creation(showing) of my Windows to my WindowFactory, that attaches a ViewModel to the View and shows the window. It is injected into my MainViewModel Constructor parameters.</p>
  
  <p>The factory itself is very simple:</p>
  
  <pre><code>public class ProductionWindowFactory : IWindowFactory
  {
      public void CreateNewWindow()
      {
          PhoneWindow window = new PhoneWindow();
          {
              window.DataContext = new phoneWindowViewModel();
          }
          window.Show();
      }
  }
  </code></pre>
  
  <p>Now, I'm aiming at implementing more functionality of my new windows, that will happen on certain button clicks <strong>IN</strong> that new window.</p>
  
  <p>As I am fairly new to Factories in general, I'm struggling to grasp a certain concept:</p>
  
  <p><strong>Example:</strong></p>
  
  <p>We have a <strong>ViewA</strong> that has a <strong>ViewModelA</strong> attached to it.
  That view has a button with a command attached, that tells our <strong>WindowFactory</strong> to show a new <strong>ViewB</strong>.</p>
  
  <p><strong>ViewB</strong> has a <strong>ViewModelB</strong> and a close button, that tells it to close <strong>ViewB</strong>.</p>
  
  <p>Now, since we shouldn't reference our <strong>ViewB</strong> in our <strong>ViewModelB</strong>, we have to somehow let it know which view it should close.</p>
  
  <blockquote>
    <p>I have come up with possible ideas / solutions, but I would really
    appreciate you letting me know which one follows the MVVM and Factory
    pattern "the most", and which one is usually used in such situations.</p>
  </blockquote>
  
  <ol>
  <li>Make our <strong>ViewModelB</strong> take an instance of windowFactory that created <strong>ViewB</strong> as a parameter on initialization, and build a method in the Factory that closes <strong>ViewB</strong> and is executed through button click -> command.</li>
  <li>Create an <strong>IWindowManager?</strong> that inherits from <strong>IWindowFactory</strong> and build a WindowManager class, that extends the capabilities of our <strong>WindowFactory</strong>, and push it in ViewModel constructors as described above.</li>
  <li>Any other correct solution, that I am completely unaware of?</li>
  </ol>
  
  <p>Please bear in mind, that the above is just an example. Ideally, I'd like to implement more of advanced functionality to my windows, and have an ability to create &amp; manage multiple different ones using that one factory.</p>
  
  <p>I have not attached much code, since I'm still at the stage of learning and deciding which solution should I go with.</p>
  
  <p><strong>EDIT - REGARDING POSSIBLE DUPLICATE:</strong></p>
  
  <p>My question differs from the proposed duplicate, as the other one is simply about managing of closing windows - My question is about doing that as well, but following a <strong>FactoryPattern</strong>.</p>
  
  <p>I have specified very clear guidelines in what I am trying to achieve and in what ways, that are completely different from the question linked. </p>