tags:

views:

58

answers:

1

Hi we have a third party application that exposes a web service for use in building websites. It handles createing sessions , getting product data, shopping cart, and check out calls to the database. I am new to MVC and my question is how do I work this webservice into my mvc site i am building, I want to take advantage of testing as well. I can not change the access to the database, I have to use the web service.

Thanks! Jon

+1  A: 

I usually deal with the data access using a services pattern. In your case I'd have an interface like IProductsService with the usual methods GetProducts(), GetProduct(id) ... and so on. By coding against that interface your controller will not care about where the data is coming from, so you would declare and use your dependency as

private IProductsService _productsService

...

IEnumerable<Products> products = _productService.GetProducts();

This comes quite handy for testing purposes too, as with IoC you can inject a different service (one that returns a hardcoded list for example) depending on your test cases. Eventually if you guys switch to a different strategy and access the data store directly you only need to create a new service, implement the interface and inject that one instead.

JoseMarmolejos
Sorry my question was broad. Lets say one of the web methods in the service is GetProducts which returns XML which represents a product. My question would be how would I go about fitting this into MVC? Do I make the api call in the controller and initialize a product object with this XML data? Or is it some where in the model? Sorry I am new to MVC and most of the examples and patterns I have seen go against the db directly.
jonb
No problem, my answer was kinda broad too. Still it applies, your controller would be calling IProductService.GetProducts() and which will return an IEnumerable<Product>, how the service gets the data and transforms it into an IEnumerable<Product> collection is of no concern to the controller.In this case your implementation of the IProductService is responsible for loading the xml file a deserializing it into an IEnumerable<Product> collection.
JoseMarmolejos
Thank you very much. I really appreciate it. It I may ask one more question, how then does the model part play into this? will I just be passing the product object around?
jonb
You're welcome, and yes in a scenario like this I usually encapsulate most (if not all) of the business logic in the services layer and use the model as a set of data transfer objects.
JoseMarmolejos