Thursday, October 1, 2009

MVC with web services

Recently, I was given the opportunity to play around with MVC

Now, my previous experience with MVC Model is with the conventional SQL database interaction using LINQ to retrieve data.

This time, however, I had to interact with web services. Trolling the web resulted in suggestion such as using a REST architecture structure and others which is quite a good read, especially the post by Rob Conery
Rob Conery - ASPNet MVC : Using Restful Architecture

I decided to experiment with using classes instead and it works. Ok, maybe the guys at the Big Blue will knock me down for this but see below for my explaination

This is what I did.

1. I built a web service library, compiled and NUnit tested thoroughly.
2. In the Model of the MVC Application, I added a "Code File". This resulted in an empty C# file being created.












3. From scratch, I built my Web Service Layer (WSL) class (I wonder if you can just add a class from the "Add") with the required properties to interact with the Web Service library.




    1 using System.Collections.Generic;


    2 using Your_WebService_ObjectName;


    3 using Your_WebService_ObjectName.WSCustomObjects.Availibility.Responses;


    4 using Your_WebService_ObjectName.WSObjects.ResLinkAvailibility;


    5 


    6 namespace MVCDemoApplication.Models


    7 {


    8     public class Your_WebService_Layer_Object


    9     {


   10         #region Private Fields


   11         private Application _app = null;


   12         private ResourceTypes _resourceTypes = null;


   13         private List<Your_WebService_ObjectName.WSCustomObjects.Availibility.Responses.GetResourceTypeResponse>


   14             _getResourceTypeResponseList = null;


   15         #endregion


   16 


   17         #region Public Property


   18         public ResourceTypes ResourceTypes


   19         {


   20             get { return _resourceTypes; }


   21             set { _resourceTypes = value; }


   22         }


   23 


   24         /// <summary>


   25         /// A list of GetResourceType response object.


   26         /// </summary>


   27         public List<Your_WebService_ObjectName.WSCustomObjects.Availibility.Responses.GetResourceTypeResponse> GetResourceTypeResponseList


   28         {


   29             get { return _getResourceTypeResponseList; }


   30             set { _getResourceTypeResponseList = value;}


   31         }


   32         #endregion


   33 


   34         public Your_WebService_Layer_Object()


   35         {


   36             _app = new Application("GUID", "PASSWORD", "Another GUID", "<INSERT_YOUR _ URL_HERE>");


   37             _resourceTypes = new ResourceTypes(_app);


   38             _getResourceTypeResponseList = _resourceTypes.GetResourceType();


   39         }


   40     }


   41 }








4. Added a View that was strongly typed to the class in the Web Service DLL.

And the result generated is as below.

3 comments:

  1. I'm immeasurably glad you posted this, if only to put me on the right track -- my first step model is crude, just classes that inherit the parent classes from the webservice, but I'm on a tight timeline. The subject of building MVC over a webservice-based model seems to be thin on the ground on the web right now, so I'm just glad to have something that'll let me get a demo together in short order, while I use the time I buy to figure out what's the BEST way to make this happen. Thanks again!

    ReplyDelete
  2. Thinking from a 30,000 foot level here, it seems all you are doing is making a "wrapper" class for your web service and then passing all of your data access calls in the controller to that which then calls your web service. This leaves me wondering, what is the advantage of having the wrapper class at all?

    ReplyDelete
  3. The reason of doing it this way, Corey, is so that I can implement legacy web services.

    REST could be a way but this was unfortunate. I also acknowledge that it would take a lot of knocking from everyone.

    ReplyDelete