MVC Best practices when all methods come from a web service
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 call开发者_开发百科s 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
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.
精彩评论