简体   繁体   中英

c# Best way to communicate between classes

Right now I am coding an application and am thinking that there has to be a better solution to what I am doing right now.

I have a main window which shall handle the settings of the program. Then I have further classes and windows. For example a language handler class and a form that is handling the user input needed for the "main function".

However, until now I always have to pass my main window to each of this classes, because the language handler shall be able to change the main window's strings. And the other form should also be able to pass data to the main Window.

If we imagine there will be much more classes and every class needs a copy of the main window this would consume a lot of resources depending on the main window's "size".

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

Common way to do that is to use observer pattern , which in .NET is events system . Simply said, your classes subscribe to each other's events and perform action when event is raised. As noted in comment, passing references is not memory heavy, but it results in tight coupling between different pieces of your code - observer pattern addresses that problem.

Another option is to consider you classes as services. Code them to an interface and then use dependency injection (aka Inversion of Control) to build up the object graph (You tell the IoC container you want a frmSomething and it will determine what services/classes it needs and instantiate them as appropriate).

This means that:

  • you only ever have to code against an interface not an implementation
  • your code is loosely coupled (You can swap an OldTranslator for a NewTranslator and as long as they both comply to the same interface, nothing has to be changed except the configuration of the container)
  • you can develop high-level features which rely on services that haven't been written yet and your code will compile
  • You can very easily change how your app works, at run-time if needs be, by changing what classes/services are registered in your container.

Have a look at Unity for the MS-Supported DI container. Castle Windsor is a popular alternative but there are many more

It's worth noting that passing a "Copy" of the main window around as you've said is not a bad thing - You're actrually only passing a reference (effectively a pointer ) to the main window (since anything more complex than the real primitives are reference types). This means that there's very little overhead whatsoever

I would suggest you to use Galasoft or Prism MVVM implementations. There you can use their messaging service which is quite easy to use. The class that needs info just sends a message to the subscriber and they in turn can send all data needed. I think that this is the easiest way to handle communication.

in addition to the ans given by IVAN.. if we look at a higher level view without all those terminologies then you should probably create a static class which would server as InMemoryStorage and defines fields on it to save information
this what you will have complete control over what is being shared and multiple components can change it
moreover you can defined getters and setters and raise an event whenever the property is changed so that different forms or windows (views) can subscribe to the change and take action accordingly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM