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,
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.
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.
Related
I have developed some MVC applications by using Entity Framework code first approach and now I am developing a new application that will also use web services for mobile applications that we will create. So, I have trouble about the issues below. Could you clarify me please one by one regarding to the issues?
Which web service technology should I use i.e. Web API, WCF, etc? (I am using MVC5 and EF version 6 in my project)
Can I use the same CRUD methods for my web application and for web services? If so, which modifications should be made on the methods and on the other fields i.e. models, etc?
For a current MVC application where EF code first approach was used, is it better to create a new methods for web services or should the current methods be updated by adding ability to support also web services?
Thanks in advance...
I highly recommend to use Commands and Queries. It's covered in this and this articles.
The Command is simple DTO object, and it could be easily sent over the network. In this case you have control over the fields and behaviour you want to make public.
Because commands are simple data containers without behavior, it is
very easy to serialize them (using the XmlSerializer for instance) or
send them over the wire (using WCF for instance), which makes it not
only easy to queue them for later processing, but ot also makes it
very easy to log them in an audit trail- yet another reason to
separate data and behavior. All these features can be added, without
changing a single line of code in the application (except perhaps a
line at the start-up of the application).
I am using 3rd party API to get and manipulate data used for my asp.net mvc application. Since I am beginner in MVC, from my standpoint I believe that use of models component of MVC pattern in such cases is not really needed. Only need to use models in this case would be if I would like to additionally manipulate data pulled from API.
Could someone please clarify if I am missing sometime in my theory.
I'm with you on your theory. It might seem a bit overkill to create a set of classes when retrieving data form a 3rd party application. In the beginning it may seem like a lot of unnecessary work.
However, my personal opinion is to always map classes in the MVC application. My reason for doing this is to keep as clear as possible separation of concern in my applications. If you need a similar application in the future or you are changing back-end for some reason, the MVC/front-end application will be as independent as possible.
It is also nice to keep a clear separation of concern if you are working with other developers and if the application will be used for an extended period of time. Also imagine if you would like to do some manipulation of the data, like you say in your own words.
To summarise, I think it is good practice to always keep a model class in your MVC application.
I'm writting an application that has many Ajax widgets (Kendo-UI to be percise). It's starting to get messy to have all those Ajax responses without the standard controllers so I was starting to consider making each entities their own controller. If I'm taking the time to do this, I figured I might as well go foward and do those as WebAPIs since I was planning to do this in a not so close future, but hey, it would be done already...
So my question is: Is it a good practice to use an MVC application's own Web API as a Ajax Widget feeds or is there any reason to stick with standard Controllers?
I've seen some arguments about performance, but I don't think this applies to this situation. I believe it was more of a "Controller calling WebAPI" situation which has obvious performance hits. But since it's already a client side Ajax call, weither it goes into a standard MVC Controller or a WebAPI controller shouldn't change a thing, would it?
Edit
Additional information regarding the project:
I am using Entity Framework for the data access.
I have a repository pattern going on with UnitOfWork.
I am using proper a MVC structure (EF POCOs AutoMapped to DTO POCOs in the repository and fed into View Models by the controllers)
This is a MVC 4 project on .NET 4.0
There is a lot of database relationships (specially for the object I'm working with at the moment)
I don't know about "good practice", but it's certainly not "bad practice". I see no difference whether you do it in the app or a different one.
I think its a good thing but only if what you are doing in the API is kept as generic as possible to other applications and services can reuse the API.
Both the applications I have written and continue to maintain use pretty much the exact same stack as your app.
I have recently re-factored one of the applications to use the API for all the common things like lists that I'm binding to Kendo ComboBoxes etc. in my views. Its a fairly large application that re-uses a lot of the same lists such as states, priorities, complexities across various Entities and views so it makes sense to put those in the API.
I haven't gone as far as going the whole hog through. I draw the line with things like this:
public ActionResult GetAjaxProjectsList([DataSourceRequest]DataSourceRequest request)
{
return Json((DataSourceResult)GetProjectsList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
That is very specific to how the Kendo Grid wants the data back. Nothing else I have connecting to this app will use this data in this format so I keep it in the controller.
In short... I use the API for common things within the same MVC app and things that I allow to be used by other applications or services, like Excel.
[I've never used WCF before. I've been googling for a couple days and haven't found any info that makes my decision of whether or not to use it obvious to me.]
I've been developing a website using ASP.NET MVC, LINQ to SQL, and SQL Server.
Now I want to develop some mobile apps which will be fed data from the site's DB.
It has been suggested to me that I use WCF for this.
I know that if I have data facing the public internet, it can be scraped if someone really wants it, but I'd like to minimize the "scrapablility" of my data.
Since my mobile apps will probably just be sending/receiving data in JSON format, what benefits would I get from using WCF instead of just RESTful JSON-returning URI's in MVC?
And if I do implement WCF, should my MVC site be hitting those services for data also instead of using LINQ in my controllers?
I've got an ASP.NET MVC application hitting WCF. I originally developed it without WCF by having the controllers interact with a service layer that hits my repositories. A requirement came up during development that required me to physically separate the UI from the service layer.
Adding WCF was a pain in the rear. Things that worked without WCF no longer worked afterwards. For example, the state of my entities was lost upon transmission to/from the service layer making it very difficult to utilize certain features of my ORM (NHibernate). I could no longer retrieve an entity, map a viewmodel to the entity in my controller, and allow NHibernate to determine whether or not an update was necessary.
That said, the challenges associated with WCF were mostly incurred at the beginning. I don't need to revisit the configuration very often and I've gotten used to working with detached entities. I also have the benefit of physical separation and WCF is extremely flexible.
Would I use WCF if I needed web services but not the separation? I really don't know. I would probably try to make JSON action methods work because those are much easier (not to mention more fun). Keeping it simple is still a wonderful principle.
As for your MVC site hitting services? I think it's safe to say that your action methods should be very thin and there should be very little business logic or persistence concerns within your MVC project. Separation of Concerns makes it much easier to adapt and change your application.
I don't see any need for WCF. I'd consider an API area, or controller if the API is small, and deliver the data via JSON from a controller action. I'd refactor the app so that the API and your controllers use the same repositories. If you need to retrieve data via AJAX from your views, you can use the API, but I don't see any point in your controllers using them if they can take advantage of the repositories.
I am currently reading through a proposal, where this person is proposing to use WCF services to facilitate AJAX calls in a web application. They say that WCF can serialize data more efficiently.
I personally have never considered going this approach. I have always liked calling MVC controllers using jQuery's AJAX functionality, and that has never been a problem.
Now there is this proposal to use WCF for AJAX, and i'm a little sceptical. I would like to keep and open mind. To me it seems to add another layer, unnecessarily complicating things. Is this worth it?
As a side note, ASP.Net MVC currently uses the JavaScriptSerializer (even though it has been marked as obsolete post .Net 3.5) for serializing JSON data where WCF uses the DataContractJsonSerializer.
So in terms of JSON serialization efficiency, MVC will be more efficient as the JavaScriptSerializer is alot more lightweight (all be it quick and dirty) than the DataContractJsonSerializer.
HTH
+1 I'm a little sceptical too. I don't think the average punter sends large amounts of data across Ajax/jQuery and if they are I think they need to refactor a little.
So if that is the case then what possible gain could you get from using WCF over normal mvc functionality when you are serializing (maybe) 2 or 3 k of data. I think even 2k is a lot to be sending via Ajax.
Using WCF in the code behind would be a good idea but for ajax serilization, dunno about that one.
I wouldn't bother with a another layer, especially not WCF, to facilitate AJAX calls. If you're already using MVC, the JsonResult will do fine (and according to Jason Fox above, more efficient).
Unless you need a web service for operations that require a web service (i.e. interacting across the web) why bother? An MVC Controller has always done the job for me without the annoying overhead.
It'd be worth it if you're exposing the same services over additional endpoints with different bindings, but if you're just using AJAX then I'd go with an MVC controller that returns JsonResult. You can still get parameter serialization using action filters.
Unless there's specific required functionality that only WCF provides thenan MVC controller will be simpler. You won't have to worry about contracts, bindings, configuration, WCF's JSON formatting, etc.
It sound interesting in concept, but at the end of the day won't you still be returning Json? Maybe I'm missing something, but how can the Json being returned by WCF be more optimized than the Json being returned by WCF? It's like saying my XML is more optimized than yours? It's not like WCF can return binary data to the browser - you would have no way of using that data... (Or can it??) I suppose the WCF serializer could possibly be faster than the MVC one, but the bottleneck is always going to be getting this data across the wire.
Of course this is all pure speculation - we would need the proposal you read to give an accurate opinion. And it depends very much on how much data you're sending via Ajax.
WCF has its own serializer, which (IIRC) is binary, and which handles object graphs that XML serialization can't. It also shouldn't have the verbose overhead of XML. As griegs noted, that's probably an excessive concern with optimization.
Is WCF more efficient than JSON? Don't know, not sure it matters that much.
In coding, there would be a thin service layer, and the ASP.NET hosting, so that adds overhead. Whether it affects performance is a separate issue; it's just overhead that's visible, as compared to .asmx or MVC controller overhead for handling services.
I don't think you really gain any of WCF's other capabilities in this case. Using WCF for ASP.NET hosted web services restricts you to a specific binding protocol.
EDIT: Looking at the project where I used WCF for web servces, it is web HttpBinding. As noted in comments, that would be JSON. On the other hand, if you're using the ASP.NET ScriptManager tag, it builds a proxy dynamically that includes parsing the data, so you're really never aware of how the data is serialized.