17

Before you mention it I am aware that this question has been asked before but not since the launch of WCF4!

So, after a lot of reading I've decided that a RESTful architecture is the way forward to begin a data providing API. Considering the release WCF 4, ASP.NET MVC 2 and WCF REST starter kit, what is the best way to begin implementing a RESTful architecture NOW?

Me: I am very familiar with ASP.NET MVC so I'd feel quite comfortable there. My knowledge of WCF, however, is lacking.

So WCF4 or ASP.NET MVC? (or something else like the wcf rest starter kit)? Specifically I'm looking for:

  • Ease of implementation
  • I know ASP.NET MVC, not WCF. Is WCF worth the learning curve?
  • Is WCF4 overkill for REST or will ASP.NET MVC falls short at some point?
BritishDeveloper
  • 13,219
  • 9
  • 52
  • 62
  • 1
    There is not enough information to answer the question. Look at the answers already. They have no substance and are just "use this because I have". The only way to answer these questions is by comparing features with your exact requirements. Because you haven't provided anything besides "low bandwidth" which is has nothing to do with the underlying framework and "many clients" which is the very definition of REST its almost impossible to give you any solid advice. – John Farrell Nov 30 '10 at 20:39
  • @Alex :/ Odd interpretation of the question considering he's asking a for "the best" and quoting "WCF 4, ASP.NET MVC 2 and WCF REST" – John Farrell Nov 30 '10 at 20:56
  • @Alex thanks. @jfar i've tried to make it more specific for you. I just want someone to let me know if they've battled a long way down either route and found that it's perfect or flawed – BritishDeveloper Nov 30 '10 at 20:58
  • 1
    Indeed, why are you restricted to MS frameworks? This is hardly where the innovation is when it comes to web development and ReST APIs. – SerialSeb Dec 02 '10 at 00:52

5 Answers5

20

I've actually implemented or am currently using all 3 of the posted options so I'll give my take. Now that you've clarified what your looking for a little its easier to answer.

OData

OData is great for internal applications when:

  1. You are both the server and the client.
  2. You are using Entity Framework.
  3. You do not use inheritance in your models and are not expecting to query sub-collections.

Odata is awesome because you can use IQueryable on the client side. This comes with some limitations though. The two off the top of my head include that you working with inherited models is a little awkward and you can't do nested collections.

There is also an issue with not really knowing what the supported LINQ capabilities are.

I'd recommend OData is you absolutely need a service layer and only expect to do simple CRUD operations with them. The main problem with every OData problem results in a hard wall you just can't get around sometimes. The client consumer code is really the best part and if you aren't using C# to consume its probably not worth it.

Also without using EFs auto metadata support you'll be writing just the same amount of code to conform to a schema your consumers may or may not enjoy writing against. While there is a Rails wrapper for OData all of this is relatively new. I don't see OData in the wild yet besides really big MS partners.

OData authentication and filtering is also pretty bare bones ATM. You'll be writing a lot of permission related code by yourself if you need to limit data. If you ever want SELECT * FROM TABLE to be limited by permissions be prepared to write some awkward code.

MVC 2

MVC is pretty good for making a RESTful service. You have verb support and return JSONResult is as easy at it can be. The only potential downside is your coding a lot of the error handling yourself and all of your view models should inherit from a base class that shows status codes and error messages.

You also may want to tweak the view engine a little depending on how fancy or convention driven you want your message replies to be. The HUGE benefit to MVC is its very extensible and you can do almost whatever you want. I'm big into combining forms/ajax calls/and rest services into the same controller action. Implement once, get three flavors of the same operation. It would be hard to make MVC fall short because it can be twisted to do almost anything you'd need.

A big benefit to an MVC service is you can sneak a little admin UI in a the application that gets deployed along with the service. Very handy to not have two sites to deploy.

WCF REST

So I'm only using WCF rest in a very limited capacity and it seems... ok... I've used WCF for 3 years and I'm always unhappy with how frustratingly complex it is to extend it. Just like ODATA you'll run into sealed classes and unextensible caverns of functionality pretty quick if you go off the beaten path. This is in direct contrast to MVC's amount of extensibility.

The other problem is your building on top of WCF and all the insanity that goes along with it. I've always said it requires a PhD to use WCF effectively. Rick Strahl had a good article about the pain points of WCF REST. Not sure if things have changed but its worth a read.

WCF REST looks really promising and I'm using it right now I just don't know enough about it to recommend.

Main Points

  1. If you don't know your consumers then I'd assume you don't know your API. Don't build a service until you have a use case and and can code against it.

  2. MVC is the most extensible and if you are familiar with how things work under the covers you'll probably be better off than implementing a hard to extend MS thing like OData and WCF.

  3. All of the "big boys" like Facebook, Amazon, PayPal, Ebay have APIs that don't really conform to any known pattern or schema like OData. Your REST service is really what you make of it. This relates to #1. Concentrate on making it easy for a consumer to work with first.

Community
  • 1
  • 1
John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • You get an upvote just for "Main point #1" :-) So many people try and design an API based the idea that they are just exposing data and there is no need to understand how it will be used. – Darrel Miller Nov 30 '10 at 22:48
  • great answer thanks. re: #1 the web site data services are all written and delivered via a asp.net mvc website. Just got a new requirement of "we need an api for an iPhone app and maybe other stuff in the future." I know little about this area so thanks for the help! – BritishDeveloper Nov 30 '10 at 23:41
  • +1 jfar - really nice summary there. i've used wcf (http) and mvc both for this task. i like the authentication that's so 'easy' in wcf, whereas in mvc there's a lot more pain. if this were resolved in a mvc-rest implementation that took care of authentication, then i think it would be my platform of choice for all our restful services. as it is, we still have requirements that only wcf at the moment can fulfil (security wise). – jim tollan Dec 01 '10 at 09:45
  • @jim wouldn't the authentication just be handled via cookies / formsauthentication since it's just HTTP based? – BritishDeveloper Dec 01 '10 at 11:26
  • BD - in my case, i'm exposing a mixture of public (90%) and private api methods that are exposed as xml/json. this is the authentication issue that occurs - ie. there is no logon form. in the scenario of forms then this is all fine. it's the 'silent' request that needs to be handled. i'm not 100% on top of this in a generic manner yet with mvc – jim tollan Dec 01 '10 at 11:32
  • +1 for the sneaky admin UI suggestion – micahhoover Dec 11 '12 at 19:47
12

You should check out OpenRasta. It's a resource-centric framework explicitly designed for implementing RESTful architectures in .NET, and features strong support for things like HTTP content negotiation and digest authentication.

OpenRasta's approach is that instead of implementing your API in terms of verbs (actions), your API should be defined in terms of resources (which will typically map closely onto your API entity model) and codecs, which provide decoupled serialization/representation of those resources as XML, JSON, HTML or any other content format your API needs to support.

It's open source, written entirely in .NET, includes built-in support for IoC and dependency injection (that's how lots of it's wired together internally), and distributed under the MIT license.

Version 2.0 has been stable for a while now, and is being used in production at several places - most notably Huddle.

In my opinion, OpenRasta's strong focus on resources means it's much closer to Roy Fielding's original vision for RESTful architecture than many of the "multi-purpose" web/HTTP frameworks, including WCF and ASP.NET MVC.

Dylan Beattie
  • 53,688
  • 35
  • 128
  • 197
  • 1
    My experience is that mapping resources to entities is a bad idea. I have had much more success treating resources like ViewModels or DTOs. – Darrel Miller Dec 02 '10 at 01:04
  • Ah - that's what I meant by "API entity model" as opposed to "domain model"... you're absolutely right, I just didn't explain it terribly well. – Dylan Beattie Dec 02 '10 at 02:35
7

Before you make any decisions, be aware that HTTP/REST support in WCF is in the process of changing significantly. See http://wcf.codeplex.com/. There will be a large amount of backwards compatibility but the new library is a complete re-write from the perspective of HTTP and WCF.

Also be aware that OData, although useful for a specific subset of applications, is not a general purpose REST framework.

If you need something .net, now, and you really want to do REST right, look at OpenRasta. If you only want to do REST because it has good marketing buzz then ASP.NET MVC will probably be good enough for you. If you are just in experimental stages then I'd keep watching the new WCF libraries.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
1

I've used the WCF Rest Service Application Template available in the Visual Studio Extension Manager with a lot of success. If you are looking to get started fast that is where I would go.

Tom
  • 130
  • 1
  • 5
1

If your project is built on top of an ORM Framework or you can hold all your data in memory collections OData is the way to go. If not (i.e. you get your data via stored procedures or something similar) go for WCF HTTP Services (a.k.a. WCF REST)

OData is really promising and has great flexibility built on top of the IQueryable interface. Actually it is something like RESTful LINQ. However unless you have some ORM underneath you will have to implemet the IQueryable stuff yourself which is almost like implementing an ORM. At this point you'd better use the lower level WCF HTTP services that give you more control and you can shape your resources the way you want. Clients libraries will not be as powerful but will not expect the service to implement all the query operators.

Stilgar
  • 22,354
  • 14
  • 64
  • 101