ASP.NET MVC & Web Services
Asked Answered
K

7

48

Does adding a Web Service to my ASP.NET MVC project break the whole concept of MVC?

That Web Service (WCF) depends on the Model layer from my MVC project to communicate with the back-end (so it looks to me like it needs to be part of the MVC solution).

Should I add this to the Controller or Model layer?

Kalamazoo answered 23/9, 2008 at 3:24 Comment(0)
T
27

It sounds like you should split out your model into its own assembly and reference it from your MVC-application and WCF-application.

  • YourApp.Data -- Shared model and data access maybe
  • YourApp.Web -- If you want to share more across your web-apps
  • YourApp.Web.Mvc
  • YourApp.Web.WebService

If you want to do WebServices MVC-style maybe you should use MVC to build your own REST-application.

Toronto answered 23/9, 2008 at 14:23 Comment(2)
Wouldn't you be duplicating controller logic in the service, then? And saving nothing by using a model, since you end up writing controller code twice.Dextroamphetamine
@Rob, no because the controller code for web page viewing should be dealing with views and validating models, etc. The restful api controller would ideally be spitting out json and formatting data.Brutal
B
19

Is there a specific reason you need to add web services to your MVC application? Unless there is a specific reason you should use your controllers in a RESTful manner just as you would a RESTful web service.

Check out this post from Rob Connery for more information: ASP.Net MVC: Using RESTful architecture

Bilabiate answered 23/9, 2008 at 3:36 Comment(2)
This is quite old, but you may have an answer for a question I have. I'm under the impression the in order to dynamically load, say, a dropdownlist with values that change when another dropdown changes, I'd need a web service. This is how I'd do it in webForms. How is this done in MVC?Nannana
The link is broken and I was unable to locate an updated link. The content is available on the Wayback Machine: web.archive.org/web/20130119131317/http://wekeroad.com/2007/12/…Sacaton
L
11

Separating the Model into it's own project is not breaking the "MVC" pattern. First off, it is just that -- a pattern. The intention of the MVC pattern is to clearly delineate between your data, the data handlers, and the presenters and the way you interface between them. The best way to do it is how Seb suggested:

  • YourApp.Data
  • YourApp.Web.Mvc
  • YourApp.Web.WebService

Something that might help you out is the MVC Storefront that Rob Conery put together. Go watch the video's here:

MVC Storefront Video Series

And if you want to look at the actual code in your browser to quickly see how he did it, go here: MVC Storefront Codeplex Code Browser

Lead answered 25/2, 2009 at 14:11 Comment(0)
H
2

I don't think separating the model into it's own assembly has any bearing on whether or not you're using MVC, you still have a model. Where it is is irrelevant surely?

Hyphenated answered 3/10, 2008 at 10:8 Comment(0)
F
1

I've had a go at doing this.

See my result at my blog

ps: I don't believe that this will break the MVC concept so long as you think that a web service is the model of a repository because all a web service does is returning a XML dump.

Foreshorten answered 1/10, 2009 at 2:52 Comment(0)
T
0

I have added web services to my application and it works well. I don't believe it violates MVC because it is an alternative interface to your model. MVC is not appropriate for web services because web services don't have a view.

Thoroughwort answered 23/9, 2008 at 5:9 Comment(1)
Web services can be created using MVC just as well as websites or RSS feeds. Actually, this is one of the strengths of the MVC concept - the same controller action can pass it's data to any kind of view - the web service outputs stuff so it is a view. :)Damian
O
0

Think of web services and databases as one in the same. Under this analogy, I think it makes sense to place your web service ingteractions where you place your database logic.

Obla answered 22/3, 2011 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.