ASP.NET Web API Vs WCF? What should i use for my project?

Microsoft defines ASP.NET Web APIs as

a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices.

ASP.NET Web API is an ideal platform for building Restful applications on the .NET Framework.

But wait… We do have WCF as part of .NET. So what is this Web API all about? When should I use this?

I was looking for some guidance to use during the solution process, but couldn’t find much help. After a bit of struggle, landed up in this web page which talks about features and when to use in detail.

From Microsoft Site

WCF ASP.NET Web API
Enables building services that support multiple transport protocols (HTTP, TCP, UDP, and custom transports) and allows switching between them. HTTP only. First-class programming model for HTTP. More suitable for access from various browsers, mobile devices etc. enabling wide reach.
Enables building services that support multiple encodings (Text, MTOM, and Binary) of the same message type and allows switching between them. Enables building Web APIs that support wide variety of media types including XML, JSON etc.
Supports building services with WS-* standards like Reliable Messaging, Transactions, Message Security. Uses basic protocol and formats such as HTTP, WebSockets, SSL, JQuery, JSON, and XML. There is no support for higher level protocols such as Reliable Messaging or Transactions.
Supports Request-Reply, One Way, and Duplex message exchange patterns. HTTP is request/response but additional patterns can be supported through SignalR and WebSockets integration.
WCF SOAP services can be described in WSDL allowing automated tools to generate client proxies even for services with complex schemas. There is a variety of ways to describe a Web API ranging from auto-generated HTML help page describing snippets to structured metadata for OData integrated APIs.
Ships with the .NET framework. Ships with .NET framework but is open-source and is also available out-of-band as independent download.

In Summary

  • Use WCF to create reliable, secure web services that accessible over a variety of transports.  If you have an existing WCF service and you want to expose additional REST endpoints, use WCF and the WebHttpBinding.
  • Use ASP.NET Web API to create HTTP-based services that are accessible from a wide variety of clients. Use ASP.NET Web API if you are creating and designing new REST-style services.  Although WCF provides some support for writing REST-style services, the support for REST in ASP.NET Web API is more complete and all future REST feature improvements will be made in ASP.NET Web API.

Couple of Must Read blog entries on this topic
http://www.codeproject.com/Articles/341414/WCF-or-ASP-NET-Web-APIs-My-two-cents-on-the-subjec

http://idesign.net/articles/asp_net_web_api_vs_wcf.htm

Happy Learning!!!

Advertisements

Single Page Application – What is it all about?

I heard the term Single Page Application for the first time sometime in March, 2012 from Vaidy. Though, I read a little bit about it at that time, I never thought it as something mainstream till the time I saw the new Hotmail application a couple of months back. my first impression was WoW. I am really impressed with the way Hotmail works now and IMHO it is much better than any of the email applications in the market.

So what is this Single Page Application (SPA) all about?
A single-page application is a web application or web site that fits on a single web page with the goal of providing a more fluid user experience akin to a desktop application. The main aim of the SPA is to make the web application work seamlessly. All the necessary code is retrieved with a single page load and changing only elements on the page, depending on the actions taken by the user. The page does not automatically reload during user interaction with the application, nor does control transfer to another page.

How does this really help?
SPA is all about providing better user experience. It ensures a fine-grained control over the user experience as it eliminates network latencies and disruptions caused by page redraws, which result in a more native-app styled experience to the user.

Characteristics of a Single Page Application
• Almost no page reloads, UI built by JavaScript
• Most of the logic is executed on the client-side
• Richer Interaction between UI Components, which makes the web application work seamlessly
• No URL Change other than #
• Back Button works as expected
• Bookmark-able Links
• Ability to go Offline

Single Page Application

How does it really work?
The first time you access the SPA the application does the following
1. Downloads all the HTML, CSS and JavaScript.
2. Process the JavaScript.
3. Download the data from server.
4. Download templates needed to render the data.
5. Generate HTML from the template.
6. Inject HTML into the DOM.

I will try to post the following in the future posts

  1. How do i decide whether i need a SPA?
  2. What are the differences between SPA and Rich Internet Plug-In Apps?
  3. What is the technology stack should I choose for building my SPA?

Modern Web applications require different skill set. HTML5 and JavaScript will become the order of the day. IMHO, If you are already into this you will at least have a better mileage for the next couple of years.

Some Useful Pointers

Single Page Interface Manifesto
ASP.NET SPA
Building Single Page Apps with ASP.NET MVC4
Building Single Page Applications with RoR
Single Page Application Architecture
Single Page Application with Backbone.js and ASP.NET Web API
Sharepoint as SPA
Trello Architecture
Architecting Single Page Front end Applications
SPA
JS Web App Essentials
Single Page Web Apps with Backbone.Js

Happy Learning!!!

Why should i care about Windows 8 and Metro Style Applications?

Windows 8 brings a new style of development called “Metro Applications”. If you are a developer programming in .NET, there is something new you have to learn.

As a .NET developer, why should you care about Windows 8? Your knowledge will be applicable for building Desktop, Tablet and Windows Phone applications. Hence, you should definitely care about it.

Unlike traditional desktop apps, a Metro style app has single, chrome less window that fills the entire screen by default, so there are no distractions. A Metro style app can support different layouts and views to create a fluid and harmonious experience across a variety of form factors and display sizes.
http://msdn.microsoft.com/en-us/library/windows/apps/hh974576.aspx

Windows 8 Platform and Tools

Source: http://dougseven.com/2011/09/15/a-bad-picture-is-worth-a-thousand-long-discussions/

You will be able to develop Metro style applications using

  • XAML and .NET(C#/VB)
  • XAML and C++
  • HTML and JavaScript
  • DirectX and C++

I am really interested to learn, how to build metro applications using HTML5, CSS and Javascript. Windows 8 has a JavaScript processing engine called Chakra and has a native JS support in the form of WinJs. Metro style applications will interact with the new runtime called WinRT and using the APIs you will be able to interact with operating system. I guess it’s more in the line of what they have already done for Silverlight and Windows phone 7.5. Check for .winmd extension for WinRT API (Metadata).

Why am I talking about HTML/Javascript way of developing metro style applications and not the C#/VB way? I somehow started getting a feel that HTML5 will become the way of developing anything and everything and the ecosystem around HTML5 will become bigger and bigger.

We have only talked about new things. If you haven’t read anything about Windows 8 before, you must be wondering by now that what happened to my .NET framework. Metro style applications and standard desktop applications will run on top of .NET framework 4.5 runtime.

Happy Learning!!!

Useful Pointers which can help you get started
WinRT demystified
A few facts about Windows runtime
WinJS : Windows Library for Javascript
What’s a metro style app?
Design case study: Website to Metro style app
Deep dive into Windows RT

Language Oriented Programming : Write your own language to develop a product

I had the opportunity to attend Martin Fowler’s Session in Bangalore last week where he talked about Domain Specific Languages.

Sendhil was telling me to read Martin Fowler’s Book on Domain Specific Languages for quite some time and for some reason I could not read. Fowler gave a good overview of DSLs in 20 minutes and I really liked the way he explained.
I did some reading about DSLs over the weekend and wanted to summarize my learning’s.

Language oriented programming (LOP) is a style of computer programming in which, rather than solving problems in general-purpose programming languages (like C# or Java), the programmer creates one or more domain-specific languages for the problem first, and solves the problem in those languages.

Wikipedia defines the concept of Language Oriented Programming as the approach to capture requirements in the user’s terms, and then to try to create an implementation language as isomorphic as possible to the user’s descriptions, so that the mapping between requirements and implementation is as direct as possible.

Let us use Regular Expressions as an example and explain what it means.
Assume, in your application you would like to validate e-mail address in multiple places. If we do not use Regular Expressions, then you have to write a code to validate the email address. It’s not something possible, but you have to write lot of code to accomplish this. There is no guarantee that this piece of code will be clean and easy to maintain.

Domain Specific Language is a programming language that is dedicated to a specific problem domain. You can also call it as mini-language. If you take the example of Regular Expressions, it’s a mini language specifically dedicated to Text processing (as a problem domain).

Another Example
Let us say you are using MS CRM – Fetch XML to Retrieve the Marketing List a contact is subscribed to. The Fetch XML would look something like this.
<fetch distinct=”false” mapping=”logical”><entity name=”list”><all-attributes/><link-entity name=”listmember” to=”listid” from=”listid” link-type=”inner” alias=”listmember0″><link-entity name=”contact” to=”entityid” from=”contactid” link-type=”inner” alias=”contact0″><filter type=”and”><condition attribute=”emailaddress1″ operator=”eq” value=”abc@abc.com” /></filter></link-entity></link-entity></entity></fetch>

Same code can be written as the following using LINQTOCRM
var contactsMarketingLists =
from marketingList in xrm.MarketingLists
join mlm in xrm.MarketingListMemberRelationships
on marketingList.ListID equals mlm.MarketingList
join contact in xrm.Contacts
on mlm.Entity equals contact.ContactID
where contact.Email == “abc@abc.com”
select marketingList;
Example borrowed from LINQ Queries using the XrmDataContext.
If you are developer, you will know which one is easy to maintain.

Are these new?
No, the term DSL is only used for last few years, but the concept is been around for quite some time.
SQL is one of the most used DSL in the world. Martin Fowler was mentioning about UNIX and the languages written in UNIX. People who were programming in LISP and Small Talk have been doing something like this for quite some time.

If you are a .NET Programmer, there is no way you would have not used WEB.CONFIG or APP.CONFIG files. Even though, the configuration is written in XML (General Purpose Language), the contents of these XML files need to conform to a specific Document Type Definition (DTD) or Schema Definition. The Schema or DTD defines the structure of the configuration XML file. Ideally if you look it is nothing but a DSL for configuring .NET applications.

Are you still not convinced?
Take the example of CSS. As a web developer, one would have used it at some point of time. Cascading Style Sheets as a DSL is focused on the Web Development as the domain. People who are specialized in this are called UI Designers. CSS as a DSL addresses the Problem domain of Page Styles, Layout etc…

Example of DSLs:
SQL, CSS, REGEX, HQL (Hibernate Query Language), LINQ, ANT/MS Build

Why do I really need a DSL? Why can’t I continue to do everything in my General Purpose Language?
In his book DSLs in BOO, Ayende has talked about the following as the reasons for developing your own DSLs
1. Making a technical issue or task simpler.
2. Expressing rules and actions in a way that’s close to the domain and understandable to businesspeople
3. Automating tasks and actions, usually as part of adding script-ability and extensibility features to your applications

In my opinion, the objective of DSLs is to create simple code which can be understood by your business analysts.

Say for example, you have written a code to parse Delimited files (Fixed Length), fetch and write it into a database. We have been writing code in our general purpose language for this for a very long time. We all know how the code looks like irrespective of how much care we take to develop it. If your business members have to review your code to figure out whether all the scenarios are covered, it will be very difficult to do it especially in the curly braces world (Java, C#) 🙂

Is there anything which is really stopping you from doing what we have been doing? No, the point is if simple and clear code is what you strive for, if communicating the Intent is more important for the business then DSLs are the way to go forward.

Can I accomplish everything in DSLs?
A domain specific language is a limited form of computer language designed for a specific class of problems.
No, by definition it’s a language with restrictions. You cannot do everything in a restricted language.  Martin Fowler talked about creating Multiple DSLs for a project, combine them together & stitch them using a General purpose Language and deliver.

Ok, writing my own language sounds great conceptually. I do not think it’s everybody’s job. Don’t you require special skills to do it?

True, Martin fowler classified DSLs into 2 types.
1. External DSL
2. Internal DSL

External DSLs are the ones which operate outside your general purpose programming language’s scope. They are written from scratch. It starts with defining the syntax of your own language and defining the language capabilities. What it means is that you have great opportunity to define everything by yourself, but you also need to handle everything yourself.

Building this is kind of defining your own general purpose language which also means understanding of compilers etc. is important. If you are from the Application Development Environment I am not sure how much can be achieved in this.
SQL, REGEX, CSS, ANT are good examples for External DSLs.

Internal DSLs are developed using an existing general purpose language. LINQ and HQL are good examples of Internal DSLs. It’s developed on top of or using C# (General Purpose Language).

Martin fowler in his blog post defines Internal DSLs as particular ways of using a host language to give the host language the feel of a particular language. This approach has recently been popularized by the Ruby community although it’s had a long heritage in other languages – in particular Lisp. Although it’s usually easier in low-ceremony languages like that, you can do effective internal DSLs in more mainstream languages like Java and C#.

Dynamic Languages provides good support for writing DSLs (Haven’t seen much of this in the .NET world using DLR) and one doesn’t have to know the compiler design for writing these languages as the host language takes care of everything. Check this post in MSDN on Internal Domain Specific Languages by Jeremy Miller.

I also found one more category in Ayende’s Book. Graphical DSLs are DSL that uses shapes and lines to express intent rather than using text. If I think now, I will agree with his definition. Example for the Graphical DSL Tools are
1. UML
2. SQL Server Integration Services
3. Windows Workflow Foundation

All these in theory sound good. Prakash, do you have any real time examples of it?
In our CP days we developed something called Query Object Model which made our job very easy to write queries.
An Example of QOM from our old demo
PeopleWhoDoNotWork => Where.Employee.Title.Contains(“Manager”).
In one of the product development engagement, the customer has built a DSL using XML and integrated it within Visual Studio.NET. All the UI Development happens through that DSL.

Sounds Great, Do you see any disadvantages?
Developers like developing DSLs. But when it comes to consuming, I have heard most of the developers complaining a lot about these. They are built on top of XMLs, I can write better language etc.

There is a significant learning curve in learning a new DSL. If your developers are not up to mark it will become a nightmare in terms of maintenance.

In Summary, I love the idea of having my own language for the development. In your organization, if you care for simple and maintainable code, they are the way to go forward.

If you are in Bangalore and if you feel that you have missed Martin Fowler’s Session, you can watch one of Fowler’s DSL talk in INFOQ site (Introduction to Domain Specific Languages)

Following are the material I read for this post.
First three chapters from this book DSLs in Boo: Domain Specific Languages in .NET
First Chapter from the book Groovy for Domain Specific Languages

Some useful posts
Domain Specific Language
Building Domain Specific Languages in C#
Overview of DSL Tools
7 recommendations for Domain Specific Language design based on Domain-Driven Design
The ‘Language’ in Domain-Specific Language Doesn’t Mean English (or French, or Japanese, or …
Writing Your First Domain Specific Language
Internal Domain Specific Languages

Happy Learning!!!

Screencast: C# 3.0

Screen cast on C# 3.0. Good one.

 

Topics Covered

·         Automatic Properties

·         Implicitly typed variables

·         Object Initializers

·         Collection Initializer’s

·         LINQ & Anonymous Types

·         LAMBDA Expressions

·         Extension Methods

·         Partial Methods

 

http://flash.timrayburn.net/introtocsharp3