1

Currently, I'm having hard time trying to find a proper way designing event system for an API.

Situation: There are few classes (named WPoint, CPoint, OPoint...) which contains a lot of primitive type variables. All variables have properties and "changed" events. There is a class (named Frame) which contains a lot of primitive type variables and also few variables of type WPoint, CPoint... and such. Again, all variables have properties and "changed" events including variables of type *Point.

Finally, there is a class named FrameBox which is basically a WinForm-Control that contains a variable of type Frame (frame) and properties that gets/sets elements of the frame. FrameBox class have some *PointElement properties to simplify reaching some frequently needed data in frame. Likewise, it has *PointElementChanged events so one can connect them to event methods in designer.

Problem: All data needs to be fully accessible for other components to make the whole stuff going, and all system system blow up because I want to make event call-back to outer classes, here is an example:

frameBox.frame.wpoint.x = 4;

Above code is not only expected to call frameBox.frame.wpoint.XChanged event but also frameBox.WPointXChanged event. As the data needs to be modifiable we should be able to do frameBox.frame = new Frame() and frameBox.frame.wpoint = new WPoint(). Having one property and event for each field in Frame.* would be an overkill and not a solution as there are too many variables/events.

Let's say we registered our events (to sub-class events) at the constructor of FrameBox frameBox.frame.wpoint.XChanged += frameBox.WPointXChanged, when a simple assign happens frameBox.frame.wpoint = new WPoint() we no longer have frameBox.WPointXChanged registered. I cannot force *Point constructor to get event parameters as I stated before there are too many events and it would result with code bloat.

Looking forward for any help, thanks anyone for spending time on helping me solving this!..

Ahmet Sait
  • 21
  • 7
  • 1
    Sounds like a design issue, you shouldn't be using an event for each variable change, you should implement `INotifyPropertyChanged`. Also, it seems like maybe a lot of your *Point classes could be consolidated into a single generic. On top of that, you may want to consider why something needs to know about something so deep in the implementation, maybe you are coupling too much of your models and your views and straying from a good MVVM or MVC design pattern. You could use event bubbling instead of nesting also. – Ron Beyer Aug 18 '15 at 01:32
  • @Ron A few googling showed me [the right way](http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist) of implementing properties but still not sure if I can solve that 'outer call back' issue... – Ahmet Sait Aug 18 '15 at 03:07

0 Answers0