I'm using MVC in a c# project, my controller knows about the model but i want the model to be unaware of the controller. So, to notify the controller about a changement, the model fires an event that the controller is subscribed to. This way i thought the model didn't know about the controller, but does it ? Is the event system an abstract layer over a callback function that the publisher calls itself when an event occurs(in my case does the model calls the controller's function ?) or does the publisher of the event really fires an event and doesn't know about who receives it?
Asked
Active
Viewed 117 times
0
-
1The .NET event system is a very simple *publish/ subscribe* (aka *observer pattern*) system. By creating a public `event`, a class *publishes* the event. When an object adds a `delegate` to the an object's event internal subscriber list, that object subscribes to the event. At some later time, when the publisher object *raises* the event, all subscribers (if any) are notified. The publisher doesn't care if there are 0, 1 or many subscribers nor who they are, it's just raising the event. In your case, the model will call the controller thru the delegate, but it does so blindly – Flydog57 Nov 21 '20 at 22:32
-
The model should be unaware of the controller anyway, you don't need any of this convoluted event publishing. – ADyson Nov 21 '20 at 23:09
-
Of you want a standard .NET way of doing this, look at the INotifyPropertyChanged interface (https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged) – Flydog57 Nov 22 '20 at 02:23
1 Answers
0
The class exposing the event doesn't know who the subscribers are.
Using an event is a correct implementation choice to respect your condition:
my controller knows about the model but i want the model to be unaware of the controller
You can validate this by checking the class coupling. You'll see that the class exposing the event is not dependent on the class that subscribes to the event. If both those classes were in separate assemblies, the controller assembly would need a reference to the model assembly, but not the other way around, as you would expect.

Batesias
- 1,914
- 1
- 12
- 22