Forwarding Eventa from the depths of classes in the form

The question was asked: 49 years 10 months ago   views: 17
0

Good evening. A small question on the application architecture, in this case, C# (.NET 2.0, older versions are not accepted).

The main window creates a class A. the Class A, within itself, creates a private class b Inside class B, there are private BackgroundWorker, which generates the RunWorkerCompleted event

Task - when there is an event on the form should be called my handler, OnMessageReceived. I mean, I like that you want to subscribe from the form on the event which is so deeply buried and, moreover, even hidden.

Moreover, I want to create a flexible enough architecture to today it worked in WinForms, WPF tomorrow, and the next day, to execute A class as an Assembly. That is, the form does all of the for example, it is important that should be called with my processor.

Of course, come up with options, like get-the properties in classes A, B, which will return the event for the opportunity subscribe to it. Or creating, again, in all classes of set-properties, for forwarding delegate function I need.

But I want to know how to do it properly. How, in terms of encapsulation, to push the variables/links in a few classes, without losing the flexibility of the architecture?

And is there any book, which describes such moments and best practices? Or just to fill your cones?)

[UPD] (the text above is slightly modified to remove unnecessary classes are the same) .NET 2.0 is due to the enterprise. It's all very long and slow change. So, only 2.0, only hardcore.

Now about the design. In fact, the program rather training. Is sending and receiving messages (hereinafter "chat") via RS232 using the class SerialPort.

In the main window is the main class of the chat (A) not to write the logic in the molds.

Further, the class chat, in particular, creates a class for receiving messages (B). In addition, creates a class for creating and sending, connect/disconnect to the port, that is, adhere to the principle "One class - one task".

But back to our hierarchy. Within a class receive stream B is class C with one method and passed to the BackgroundWorker. This class is needed for transmission of a large number of parameters in the flow, read more on MSDN.

It turns out that formy inside a class, inside a class another class, inside another class BackgroundWorker with my desired eventim.

Asked: 01-01-1970 в 03:00:00
C# 2.0? Stop raping the corpse. - 02-02-2013 в 23:01:34
Most likely, it's Bad design. What is the class hierarchy A->B->C->D? - 02-02-2013 в 23:01:46
That's it, your design is bad. For starters, you should be in class A events OnWorkerComplete, no wonder that some internal operation today has BackgroundWorker'om. Carry out the semantics, not the implementation details. What class And wants to tell the world when BackgroundWorker makes it to the end? - 02-02-2013 в 23:09:59
Said task. Yes, the example OnWorkerComplete not very good, let it be neutral OnMessageReceived. It does not matter. It is important that this event occurs far below, and me upstairs, you need to notify the user. - 02-02-2013 в 23:20:45
That is, the shape class contains a class MyChatProgram, which wrapped up the ChatClient class, which hides the class MessageSender (although he's not just a sender, but also to connect able). And there's something else. The design is bad. - 02-02-2013 в 23:58:57

Answers   1

0

It is not necessary to raise it the event of the worker. If you experience sobti worker-class B have to throw your event with their arguments. In response to this event class And need to throw your event. And here on the event class And should sign a form to its handler. In fact, classes a, b and the worker are different levels of abstraction, and they should not leak into each other. The only way your architecture is flexible enough to add post-processing of events on each level and painless to change it. That is, the form does not need to know anything about the events of the worker. Form should not know about the existence of a worker.

Answered: 22-10-2015 в 16:44:24