1

I work with C# and WPF and not using MVVM approach. One of the important part of applications is communication among Windows Pages and UserControls. In MVVM they do it behind the scenes and let you use some method to make it possible to send or receive data among different instances.

In code behind, Parent and child windows can communicate easily by just passing or assigning ParentWindow reference to ChildWindow. To make communication possible among different windows or pages. But what I've done is,

Created a Static Class named as Operator.cs that has a List<Window> AvailableWindows . Now every time we create a window, we push this instance into List<Windows> AvailableWindows and when closing window remove that instance from AvailableWindows . This way I've reference to available pages or windows and from anywhere i can access it and do whatever and can communicate among many instances easily.

public static class Operator
{
    public Operator()
    {
    }
    public static List<Page> AvailablePages = new List<Page>();
    public static List<Window> AvailableWindows = new List<Window>();

}  

My question is, if it's a good approach, bad or just Ok ? or any other alternative ?

1 Answer 1

2

It really depends on what it is you are trying to accomplish by having the pages being able to reference each other. I would tend to be concerned about a separation of concerns with pages eventually being spaghetti coded together, with page A reaching over into window B which reaches over to page C which reaches back to page A, etc.

If the use cases are event-like, I would probably use the mediator pattern (https://sourcemaking.com/design_patterns/mediator) to allow the individual pages to pub-sub to events. While this pattern is often used in MVVM as well, it doesn't rely on MVVM. You also seem similar ideas in JavaScript webapps, so it isn't technology-specific.

2
  • What i'm doing is also something like Mediator like pattern. isn't it ? the Operator class acting like an mediator and helping me communicate with other instances. And that's what i'm doing here (communication e.g doing somthing on one window that should reflect some results on other window).
    – Rao Arman
    Oct 17, 2020 at 6:58
  • 1
    It's similar, the advantage to the mediator is that by avoiding direct pointer access between windows, you can be far more precise about what information is needed and keep it to a minimum. My guess would be that the operator class would work fine on day one and eventually get spaghetti like, but either will work with sufficient discipline.
    – Michael
    Oct 19, 2020 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.