C # Best way to communicate between classes

Now I am coding the application, and I think that now there should be a better solution for what I am doing right now.

I have a main window that should process the program settings. Then I have additional classes and windows. For example, a language handler class and a form that handles user input necessary for the "main function".

However, so far I always had to pass the main window to each of these classes, because the language handler should be able to change the lines of the main window. And another form should also be able to transfer data to the main window.

If we assume that there will be many more classes, and each class needs a copy of the main window, this will consume a lot of resources, depending on the main size window.

So, is there a better / more efficient way to communicate between these classes.

+7
source share
4 answers

The general way to do this is to use the observer pattern , which in .NET is an event . Simply put, your classes subscribe to each other's events and perform actions when raising an event. As noted in the commentary, passing links is not very difficult for memory, but this leads to a close relationship between the various parts of your code - the observer pattern template. This problem.

+9
source

Another option is to consider classes as services. Match them with the interface, and then use aka Inversion of Control to create a graph of the object (you specify the IoC container that you want frmSomething , and it will determine which services / classes it needs and, if necessary, create an instance of them .)

It means that:

  • you only need to execute the code with an interface, not an implementation
  • your code is loosely coupled (you can change OldTranslator to NewTranslator and as long as both of them correspond to the same interface, nothing needs to be changed except the container configuration)
  • you can create high-level functions that rely on services that have not yet been written, and your code will compile.
  • You can very easily change how your application works, at runtime, if necessary, by changing which classes / services are registered in your container.

Check out Unity for an MS-enabled container. Castle Windsor is a popular alternative, but there are many more

It is worth noting that the passage of the β€œCopy” of the main window around, as you said, is not so bad: you just pass the link (actually a pointer) to the main window (since everything that is more complicated than real primitives is reference types). This means that there is very little overhead

+3
source

I suggest you use the Galasoft or Prism MVVM implementation. There you can use your messaging service, which is pretty easy to use. A class that needs information just sends a message to the subscriber, and they, in turn, can send all the necessary data. I think this is the easiest way to handle communication.

+2
source

in addition to the ans provided by IVAN .. if we look at a higher-level view without all of these terms, then you should probably create a static class that will be a server as InMemoryStorage and defines fields on it to store information
it's that you will have full control over what is shared, and several components can change it
in addition, you can define getters and setters and raise an event whenever a property changes so that various forms or windows (views) can subscribe to the change and take appropriate measures.

+2
source

All Articles