Sunday, October 3, 2010

Using MVC for different server versions

Hi all,


On this blog I will try to suggest and share some design idea that i handled not too long ago. It is a very nice manipulation of MVC pattern which i created.


I will start with some quick description of the solution and problem, and finish with a nice easy example.


The motive
I needed to handle the following requests:

  1. My server should support a very large amount of inputs from several outer sources (sensors - COM connection, TCP connection from remote clients, Http requests from a web application, local GUI and more..) 
  2. The main and first issue was that i needed to implement a state machine that can support all those requests and even be extensible.
  3. Secondly, each type of input can be supported or not supported in different versions of the server.
  4. Some inputs produce the same output, but some doesn't.
Sounds tricky? 

The Solution



As i mentioned earlier, it is based on MVC so let's dive in:



  1. Model - this is actually the easy part, the model is some data structure that holds only data. No logic is allowed. (can be DB, memory based, file system, etc)
  2. View - In this section you will find the implementation of the input devices. You will find a Http server, Tcp server, Serial ports and many kinds of interfaces. In my implementation, there are not connected to anything.
  3. Controller - Now this is a little bit similar to the usual implementation of MVC but it's getting tricky. The controller is actually the core of the system, it is based on objects that has an interface with Methods and Events, but also has access to the Model only.
  4. This is where the magic begins. This is actually an additional layer - Binders. Those binders are the connection between the View and the Controller and also the version of the server.  Any input from the view is transfered through the binders to the controller. This way, you can have different binders implementation for each view, version and even replace the input and outputs in real-time without changing the main core of the system. Only binders.
Example: Future Car Wash
Lets assume that we have a car wash server. The model will be the state of the washing procedure. The controller will be a set of actions that the car wash machine can do:
Drag-In(), Wash(), Stop(), Resume(), Drag-Out().
The view will be two types of interfaces: 
  1. Sensor system - For automatic wash.
  2. Manual local GUI - For operator.
Now, Lets get into action:
Lets assume that we are binding only the automatic mode binder. The vehicle enters the station. Drag-In starts, then Wash. Suddenly the driver gets out and the sensor recognize it. Stop is invoked of course. However, because we are catching the sensor event in a Binder and not in the controller, we can easily launch the Manual Binder. The operator can ask the driver to enter the vehicle and then click Resume in the GUI. The Binder will transfer the command Resume() to the controller, The Auto-Binder will be reloaded again and after the washing stops the Drag-Out() will occur in the controller. 

What a beauty! The core of the system remained the same - all the input was changed in real time!


Best Regards,

Gur Kashi
gurkashi@gmail.com

1 comment:

  1. Really an informative article about Using MVC for different server versions.The way you wrote it makes all the points very clear the MVC patternmakes the code efficient and easy to maintain.You just need to follow the conventions. electronic signature

    ReplyDelete