Setup an ASP.NET Web Service Solution (MVC Web Service part 2)

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:

  • MVCArt.MVCWebService
  • MVCArt.Controllers
  • MVCArt.Models

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:

  1. A consumer invokes the web method exposed by the .asmx (SaveImages).
  2. The view (.asmx) creates a result object to be passed to the controller.
  3. The result object is passed as a ref parameter to the controller along with the images.
  4. The controller processes the images and fills the result object with data.
  5. 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.

Links

  • Part 1:
  • Part 3: Implementing the business logic (to be published)
Tagged with: , , , ,
Posted in .NET, ASP.NET, C#, Visual Studio
3 comments on “Setup an ASP.NET Web Service Solution (MVC Web Service part 2)
  1. lynnset says:

    i never think ASP.NET can do as Struts.
    why every Microsoft Technology is later than Java.
    am i stupid?
    but i already took MCDP Enterprise.

    now Entity Framework!
    too late; more not still good as EJB.

  2. sravani says:

    itsgood

  3. Princess says:

    Not so clear, it will be better if you explain with sample code.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>