Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use WCF with MVC?

I'm testing ASP.NET MVC 3.

And for an ajax call, I can use a MVC controller Method, or a WCF service. But why would I use WCF, if I can do it with MVC ?

My questions is : Should I Use WCF services with MVC, or not ? And Why ? And in which case ?

Thanks,

like image 376
Tkanos Avatar asked Dec 14 '12 13:12

Tkanos


2 Answers

WCF is a framework used for developing web services. The idea of a web service is that it decouples functionality used for providing raw data from functionality used for treating the data and providing it to the end-user. There are a few advantages to this:

  • If you are providing an API, you don't know how the data will be used. You want to simply provide raw data to a set of applications and let those applications handle the rest. A web service does just that... it opens up the data layer of an application while leaving the rest closed.
  • It can improve data-layer maintenability by enforcing loose coupling. Loose coupling means that the components of your application are not entwined with one another. This is a good thing because it makes it easier to make modifications to parts of your application without disrupting the rest. For example, if it is understood that a given function call will return a set JSON object, you can make changes to the table structure of the database that provides data for that object without interfering with the code of the consuming application. This works so long as you uphold the predefined data contract by always supplying the same type of data in the same format. On the other hand, if database queries, connection strings and the like are all hardcoded into your application it makes modifying your database logic significantly more difficult.

In your case, if you are just developing a small to medium-sized web application and have no intention of launching an API or similar service, there is probably no need for WCF.

Keep in mind however that while you probably don't need to write a WCF service for your application, you should still try to loosely-couple your application layers as you would with a service. You can do this by splitting data-access code or object (entity) definition code out into separate projecs. Loose coupling, whether it is implemented with WCF or just MVC makes maintaining your project simpler, easier and more affordable and is overall a very good practice to abide by.

like image 160
Levi Botelho Avatar answered Oct 16 '22 17:10

Levi Botelho


MVC is fine, you really don't need WCF for this. MVC creates some sort of REST API (all your action methods get their own URL that you can use to call the method), so you can use that.

like image 3
Leon Cullens Avatar answered Oct 16 '22 17:10

Leon Cullens