Window Factory and Manager using MVVM pattern I am fairly new to Factories and Dependency Injection in MVVM. 

I've build my View(Window) factory with help from SO, and I would like to know if I had implemented everything properly(code works just fine).

I'm sorry for the length, but there was no other way for me to show my complete example.


First, there is the WindowFactory class. Its purpose is to delegate the creation of windows and coupling it with their ViewModels, so that I can avoid referencing View in its respective ViewModel (following MVVM pattern).

public class WindowFactory : IWindowFactory
{
    private readonly IWindowManager _windowManager;

    public WindowFactory(IWindowManager windowManager)
    {
        this._windowManager = windowManager;
    }

    public void CreatePhoneWindow()
    {
        var view = new PhoneWindow();
        var viewAdapter = new ViewAdapter(view, _windowManager);
        var viewModel = new PhoneWindowViewModel(viewAdapter);

        _windowManager.addWindow(view);
        view.DataContext = viewModel;
        view.Show();
    }

    // Here are copies of the above method, one for each different window that I create
}

WindowManager class. Its purpose is to keep track of current open windows, and allow certain operations on them (like checking if any window is open at a time, so that I can prevent other windows from being created)

class WindowManager : IWindowManager
{
    private ObservableCollection&lt;Window&gt; _windows;

    public WindowManager()
    {
        _windows = new ObservableCollection&lt;Window&gt;();
    }

    public void addWindow(Window window)
    {
        if (_windows.Contains(window) == false) { _windows.Add(window); }
    }

    public void removeWindow(Window window)
    {
        if (_windows.Contains(window) == true) { _windows.Remove(window); }
    }

    public bool isAnyWindowOpen()
    {
        if (_windows.Count == 0) { return true; }
        else { return false; }
    }
}

ViewAdapter class. Its purpose is to handle View close commands, that are comming from inside the View's ViewModel (again, to decouple).

class ViewAdapter : ICloseable
{
    private readonly Window _view;
    private readonly IWindowManager _windowManager;

    public ViewAdapter(Window view, IWindowManager windowManager)
    {
        this._view = view;
        this._windowManager = windowManager;
    }

    public void Close()
    {
        _windowManager.removeWindow(_view);
        _view.Close();
    }
}

Finally, I instantiate my WindowFactory and WindowManager in my MainWindow (Main, startup/default View) code-behind:

public MainWindow()
{
    InitializeComponent();

    var windowManager = new WindowManager();
    var windowFactory = new WindowFactory(windowManager);
    MenuItemsStackPanel.DataContext = new SideMenuViewModel(windowFactory, windowManager);
}




  I'm not including the Interfaces, since they contain simply the mocks of the used functions, nothing else.


I would really appreciate any feedback on my Factory design especially, since I might have made mistakes that I am unaware of, OR there could be a much better, cleaner way to achieve this.
Disclaimer: I haven't really used MVVM though am familiar with the pattern.



WindowFactory

The layers in MVVM, from upper to lower in terms of direction of dependencies (dependencies flow down), are View, ViewModel, and Model. Therefore it seems wrong to me that the ViewModel contains a View (by way of containing a ViewAdapter).

Another issue is that CreatePhoneWindow() actually displays the window. I would expect a factory to return a window, which the client can then display (or not) as and when desired. This isn't to say it is wrong, but it feels wrong (to me).

WindowManager

if (_windows.Contains(window) == false) is unnatural; use if (!_windows.Contains(window)) instead. Same for the comparison against true in the other method.

if (_windows.Count == 0) { return true; } 
else { return false; }


This also can be simplified to return _windows.Count == 0;. (But is it correct that when the _windows collection is empty it means windows are open?)

Also why are you using an ObservableCollection? It appears you're not binding to it, being as it's a private field, so why not just use a standard List&lt;Window&gt; instead?

ViewAdapter


  Its purpose is to handle View close commands, that are coming from
  inside the View's ViewModel...


I don't understand why this class exists. If a ViewModel wants to say "time to close", it can raise an event. The View (Window) - which should have a dependency on the ViewModel - can subscribe to this event, and close itself. No need for an extra class to do its own closing. If you still want to have a WindowManager, then the WindowManager can subscribe to the Window's OnClose event and remove that object from its collection.