Questions on the MVP pattern RRS feed

  • Question

  • I am developing my largest (and most complex) Windows.Forms project to date. I am alone on the project, and in order to keep things structured, I have decided to use the MVP pattern in the design where ever possible.

    I have three pieces of hardware streaming UDP packets to my computer. When I press the a button, I would like to process and save the data in the stream based on the IP of the incoming packet.

    I have thought about making a StreamingHardwareModel to model each of the hardware units.

    When a UDP packet is received, I check the senders IP and match it to a Dictionary of IP strings. If the IP does not exist, I add the IP to the Dictionary<string, StreamingHardwareModel> and create a new StreamingHardwareModel.
    If the IP already exists, I route it to a processing method that processes and saves the data i a file. The output is three files each corresponding to the UDP data received from the three hardware units.

    Since the Model does not exist before the data is received I cannot have the Model contain the UDP "server". Where should I then put it? The Controller?
    Alternatively I have thought about making the UDP "server" a static part of the Model, and let the Dictionary be static as well. It feels more right to have the UDP "server" located in the Model as it models the hardware's UDP capabilities.

    A more general question on MVP pattern.
    I have read a few introductions to MVP (in C# specifically), and I can't seem to figure out what the common practice for starting (so to speak) the MVP.
    In http://polymorphicpodcast.com/shows/mv-patterns/ the View initializes the presenter. The Model is created at runtime when the controller issues a Save() method based on a Button from the View.
    However, this kinda rules out the possibility for the Model to raise an event to the Controller who then can update the View. This is rather essential i many projects, I presume, and is also important in my project, where the Model should raise events based on the state of the connected hardware. (It does so through a TCP server that handles communication with the hardware. Commands can also be issued to the hardware like, "reboot", "start", "stop", etc. The hardware is capable of responding to commands, and can also send data describing the hardware's state. Thus an event to handle an error flag would be extremely useful.)

    So, if the Model should exist prior to the Controller creating it (and the View creating the Controller), where then, should it be initialized?
    Thursday, November 19, 2009 9:09 AM


All replies