In this article the actual MVC like design will start to show up. The previous article, , didn’t really have anthing to do with MVC, so let’s begin!
Almost all design patterns will make re-use of code easier, but to accomplish this it is important to setup the Visual Studio Solution in such a way to support this. In the setup I will show here I will make way for re-use and the implementation of the MVC design.
How to do it
When I setup Visual Studio Solutions I like to create an empty solution, and add the projects one by one. In this setup there will be three projects:
- MVCWebService; the View, an ASP.NET Web Service
- Controllers, a class library
- Models, a class library
All three projects will be in the same top namespace, so before doing anything anything else remove the automatically created classes in Models and Controllers projects as well as the web service (.asmx) in the MVCWebService project.
After that modify the projects’ properties setting the default namespace to:
After that it is clear to go adding classes and web services to the projects. Rebuild the solution making sure the namespace changes were successful.
If the solution is setup in this way, the view, e.g. the web service project, may be replaced by something else. As stated in the introduction to this series I will maybe show how the create a WCF application using the exact same controllers and models as with the web service as a view.
I will now explain more in detail how the MVC design pattern is applied onto this solution.
The MVC design
Since there is no actual UI in the web service world, the need to define the view in the MVC design is crucial. A lot of blogs and other sites are covering the MVC design topic, but it is not a very easy to apply it to a non-UI application, such as a web service. I found a great picture on Java BluePrints (J2EE Patterns Model-View-Controller), making it fairly easy to map the MVC onto this application. I will explain the mapping below.
As stated before, the view will be represented by the web service front-end (.asmx), rendering the models in such a way that the result (MVCWebServiceResult) to be returned back to the consumer is created here.
The result will be passed to a controller along with the MVCWebServiceImages object. The images will be processed by the controller and the result object will be filled with the appropriate data in this process.
Please review the interface defined in the previous article, .
Let’s look at the processing of an incoming method invocation:
- A consumer invokes the web method exposed by the .asmx (SaveImages).
- The view (.asmx) creates a result object to be passed to the controller.
- The result object is passed as a ref parameter to the controller along with the images.
- The controller processes the images and fills the result object with data.
- The view renders the result back to the consumer by returning the result object.
As shown above there is no event handling or state changed handling in the same way as if there would have been a UI. This makes error handling and robustness very important, since the view (.asmx) will have to asume the passed result parameter to be correct and rendered OK back to the consumer.
The above may look a bit complicated, but it will for sure be much clearer when implementing the logic in the next step in this series of articles.
- Part 1:
- Part 3: Implementing the business logic (to be published)