posted Feb 9, 2009 8:39 AM by M Soni
Contents
introduction................................................................................................................. 1
differences between visual basic .net and Visual c# . net...................... 2
Case Sensitivity 2
Variable Declaration and Assignment 2
Data Types 3
Statement Termination 3
Statement Blocks 4
Use of () vs. [ ] 4
Operators 5
Conditional Statements 7
Error Handling 7
Overflow Checking 8
Parameter Passing 8
Late Binding 9
Handing Unmanaged Code 10
Keywords 10
Conclusion.................................................................................................................. 15
Because of the past differences between Microsoft® Visual Basic™, Microsoft® Visual C™, and Microsoft® Visual C++™, many developers have the impression that Microsoft® Visual C# .NET™ is a more powerful language than Microsoft® Visual Basic .NET™. Some developers assume that many things that are possible in Visual C# .NET are impossible in Visual Basic .NET, just as many things that are possible in Microsoft® Visual C™ 6.0 and earlier or Microsoft® Visual C++™ 6.0 and earlier are impossible in Microsoft® Visual Basic™ 6.0 and earlier. This assumption is incorrect. Although differences exist between Visual Basic .NET and Visual C# .NET, they are both first-class programming languages that are based on the Microsoft® .NET Framework, and they are equally powerful. Visual Basic .NET is a true object-oriented programming language that includes new and improved features such as inheritance, polymorphism, interfaces, and overloading. Both Visual Basic .NET and Visual C# .NET use the common language runtime in the .NET Framework, and almost no performance issues now exist between them. Visual Basic .NET may be oriented more toward ease of use by providing features such as late binding, and Visual C# .NET may have a few more “power” features, such as handling unmanaged code, but the differences are very small compared to what they were in the past.
This document discusses differences between Visual Basic .NET and Visual C# .NET. However, the key point to keep in mind is that .NET is intended to be language-independent. The choice between Visual Basic .NET and Visual C# .NET is typically based on your personal preference and past experience; for example, it is easier for Visual Basic 6.0 developers to use Visual Basic .NET, and for Visual C++ and Java programmers to use Visual C# .NET. The existing experience of a programmer far outweighs the small differences between the two languages.
Syntactically, Visual Basic .NET and Visual C# .NET are two different languages, just as Visual Basic, Visual C, and Visual C++ are different languages. Visual C# .NET looks more familiar to Visual C, Visual C++, and Java programmers, and Visual Basic .NET looks more familiar to Visual Basic developers. The biggest differences between the languages fall into the following categories:
· Case sensitivity
· Variable declaration and assignment
· Data types
· Statement termination
· Statement blocks
· Use of () vs. []
· Operators
· Conditional statements
· Error handling
· Overflow checking
· Parameter passing
· Late binding
· Handling unmanaged code
· Keywords
Identifier names in Visual Basic .NET are not case-sensitive, but identifier names in Visual C# .NET are. This primarily presents a problem when you write code, and is not an issue in debugging a program that already compiles.
Variables in Visual Basic .NET are declared with the variable before the data type. In Visual C# .NET, the data type precedes the variables.
|
Visual Basic .NET |
Visual C# .NET |
|
Dim i, j As Integer |
int i, j; |
|
Dim i As Integer = 7 |
int i = 7; |
|
Dim i(6) As Integer
or
Dim i() As Integer = New Integer(6) {} |
int[] i = new int[6]; |
|
Dim con As SqlConnection |
SqlConnection con; |
|
Dim x As New Y("ABC")
or
Dim x As Y = New Y("ABC") |
Y x = new Y("ABC"); |
Simple data types have different names in Visual Basic .NET and Visual C# .NET. For example, Integer in Visual Basic .NET is int in Visual C# .NET. However, System.Int32, the .NET Framework base type for which Integer and int are aliases, can be used in both languages. Visual C# .NET also supports the signed byte, unsigned short, unsigned int, and unsigned long data types, which are not available in Visual Basic .NET.
The following table lists the different data type names in each language and the base types for which they are aliases.
|
Visual Basic .NET |
Visual C# .NET |
.NET Framework |
|
Boolean |
bool |
System.Boolean |
|
Byte |
byte |
System.Byte |
|
Short |
short |
System.Int16 |
|
Integer |
int |
System.Int32 |
|
Long |
long |
System.Int64 |
|
Single |
float |
System.Single |
|
Double |
double |
System.Double |
|
Decimal |
decimal |
System.Decimal |
|
Date |
System.DateTime |
System.DateTime |
|
String |
string |
System.String |
|
Char |
char |
System.Char |
|
Object |
object |
System.Object |
|
n/a |
sbyte |
System.Sbyte |
|
n/a |
ushort |
System.UInt16 |
|
n/a |
uint |
System.UInt32 |
|
n/a |
ulong |
System.UInt64 |
Statements in Visual Basic .NET are terminated by the end of the line. You can use the colon (:) to put multiple statements in a line, and you can use the line continuation (_) character to make a statement span several lines.
Statements in Visual C# .NET are terminated by the semicolon (;). You can use multiple statements per line, and statements can span multiple lines.
Visual Basic .NET |
Visual C# .NET |
|
A = 5
B = 7 : C = 8
MySub (Arg1, _
Arg2, _
Arg3) |
A = 5;
B = 7; C = 8;
MySub (Arg1,
Arg2,
Arg3); |
Visual Basic .NET does not use arbitrary statement blocks. Instead, certain keywords that have a specialized terminating statement are used instead of the statement blocks.
In Visual C# .NET, braces ({}) are used to delimit a statement block; otherwise, a single statement is assumed.
|
Visual Basic .NET |
Visual C# .NET |
|
If A = 5 Then
DoSomething()
DoSomethingAgain()
End If |
If (a == 5)
{
DoSomething();
DoSomethingAgain();
}
or
if (a == 5)
DoSomething();
DoSomethingAgain(); //This is not part of
//the if statement. |
Visual Basic .NET uses parentheses () to delimit array elements, function arguments, and property indexes.
Visual C# .NET uses parentheses () to delimit function arguments, and brackets ([]) to delimit array elements and property indexes.
|
Purpose |
Visual Basic .NET |
Visual C# .NET |
|
Declare an array |
Dim a() As Long
Dim a(3, 5) as Integer |
int[] x = new int[5]; |
|
Initialize an array |
Dim a() As Long = {3, 4, 5} |
int[] x = new int[5] {1, 2, 3, 4, 5}; |
|
Reallocate array |
Redim |
n/a |
|
Functions Arguments |
X= A(5)
MySub (A, B, C)
|
MySub(A, B, C);
|
|
Property Indexes |
Y = MyDataSet.Tables_ ("Author").Rows(5)._
Columns("AuthorID") |
Y = MyDataSet.Tables["Author"].Rows[5].Columns["AuthorID"] |
The operators that are used in Visual Basic .NET and Visual C# .NET are quite different. The following table lists the main operators. This information can also be found in the Microsoft® Visual Studio .NET™ documentation.
|
|
Operator |
Visual Basic .NET |
Visual C# .NET |
|
|
Additive |
|
|
|
|
Addition |
+ |
+ |
|
|
Subtraction |
- |
- |
|
|
Multiplicative |
|
|
|
|
Multiplication |
* |
* |
|
|
Division |
/ |
/ |
|
|
Integer division |
\ |
/ (depending on the operands) |
|
|
Modulus (division returning only the remainder) |
Mod |
% |
|
|
Exponentiation |
^ |
n/a |
|
|
Assignment |
|
|
|
|
Assignment |
=
+= -= *= /* |
=
+= -= *= /* |
|
Integer division |
\= |
/= (depending on the operands) |
|
|
Concatenate |
&= |
+= |
|
|
Modulus |
n/a |
%= |
|
|
Left shift |
n/a |
<<= |
|
|
Right shift |
n/a |
>>= |
|
|
Bitwise AND |
n/a |
&= |
|
|
XOR |
n/a |
^= |
|
|
OR |
n/a |
|= |
|
|
Relational and equality |
|
|
|
|
Less than |
< |
< |
|
|
Less than or equal to |
<= |
<= |
|
|
Greater than |
> |
> |
|
|
Greater than or equal to |
>= |
>= |
|
|
Equal |
= |
== |
|
|
Not equal |
<> |
!= |
|
|
Compare two object reference variables |
Is |
== |
|
|
Compare object reference type |
TypeOf x Is Class1 |
x is Class1 |
|
|
Compare strings |
= |
== or String.Equals() |
|
|
Concatenate strings |
& |
+ |
|
|
Shortcircuited Boolean AND |
AndAlso |
&& |
|
|
Shortcircuited Boolean OR |
OrElse |
|| |
|
|
Shift |
|
|
|
|
Left shift |
n/a |
<< |
|
|
Right shift |
n/a |
>> |
|
|
Scope resolution |
|
|
|
|
Scope resolution |
. |
., base |
|
|
Postfix |
|
|
|
|
Type cast |
Cint, CDbl, …, CType |
(type) |
|
|
Member selection |
. |
. |
|
|
Postfix increment |
n/a |
++ |
|
|
Postfix decrement |
n/a |
-- |
|
|
Unary |
|
|
|
|
Indirection |
n/a |
* (unsafe mode only) |
|
|
Address of |
AddressOf |
& (unsafe mode only) |
|
|
Logical NOT |
Not |
! |
|
|
One's complement |
Not |
~ |
|
|
Prefix increment |
n/a |
++ |
|
|
Prefix decrement |
n/a |
-- |
|
|
Size of type |
n/a |
sizeof |
|
|
Bitwise |
|
|
|
|
Bitwise NOT |
Not |
~ |
|
|
Bitwise AND |
And |
& |
|
|
Bitwise XOR |
Xor |
^ |
|
|
Bitwise OR |
Or |
| |
|
|
Logical |
|
|
|
|
Logical AND, OR |
And |
&& |
|
|
Logical OR |
Or |
|| |
|
|
Conditional |
|
|
|
|
Conditional |
IIf |
?: |
|
|
Pointer to member |
|
|
|
|
Pointer to member |
n/a |
. (Unsafe mode only) |
|
|
|
|
|
|
|
|
Conditional Statements
The following table lists the differences in the conditional statements that Visual Basic .NET and Visual C# .NET use.
|
Conditional Statement |
Visual Basic .NET |
Visual C# .NET |
|
Decision structure (selection)
|
Select Case …, Case, Case Else, End Select |
switch, case, default, |
|
Decision structure (if … then)
|
If … Then, ElseIf … Then, Else, End If |
if, else |
|
Loop structure (conditional) |
While… End While, Do [While, Until] …, Loop [While, Until] |
do, while, continue |
|
Loop structure (iteration)
|
For …, [Exit For,] Next
For Each …, [Exit For,] Next |
for, foreach |
|
Control flow statement |
Exit, GoTo, Stop, End, Return, |
break, continue, goto, return,
throw |
Unstructured error handling is for backward compatibility. Visual Basic .NET supports both structured and unstructured error handling, but Visual C# .NET supports only structured error handling.
|
Purpose |
Visual Basic .NET |
Visual C# .NET |
|
Structured error handling |
Try
… Catch …
Finally … End Try |
try, catch, finally, throw |
|
Unstructured error handling |
On Error GoTo …
On Error Resume Next |
n/a |
Visual Basic .NET has a project level setting to check for overflow. However, the checking can only be turned on and off at the project level, instead of at the level of an expression or a block of code. To turn overflow checking on and off, follow these steps:
1. On the Project menu, click Properties.
2. Under Configuration Properties, select Optimizations, and then select or clear Remove integer overflow checks.
Visual C# .NET statements can run in either a checked or an unchecked context. In a checked context, arithmetic overflow raises an exception error. In an unchecked context, arithmetic overflow is ignored and the result is truncated. This can be used on an expression or a block of code.
Visual Basic .NET uses ByVal for passing parameters by value, and uses ByRef for passing parameters by reference. Visual Basic .NET can also force parameters to be passed by value, regardless of how they are declared, by enclosing the parameters in extra parentheses. Visual Basic .NET also supports optional parameters, which are not available in Visual C# .NET.
Visual C# .NET does not have a way to pass reference types (objects) strictly by value. You can either pass the reference (basically a pointer) or a reference to the reference (a pointer to a pointer). Unmanaged Visual C# .NET methods can take pointers just like Visual C++ methods. To pass a parameter by reference, Visual C# .NET uses the ref keyword. To use a ref parameter, the argument must explicitly be passed to the method as a ref argument. The value of a ref argument is passed to the ref parameter.
|
Purpose |
Visual Basic .NET |
Visual C# .NET |
|
Pass by value |
Public Sub ABC (ByVal y As Long)
…
End Sub
ABC(x)
ABC((x))
|
void ABC(int x)
{
...
}
ABC(i);
|
|
Pass by reference |
Public Sub ABC(ByRef y As Long)
…
End Sub
ABC(x)
|
void ABC(ref int x)
{
...
}
ABC(ref i);
|
|
Optional parameter |
Supported |
n/a |
Late Binding
Both Visual Basic .NET and Visual C# .NET can implement implicit late binding through reflection. However, implementing late binding in Visual Basic .NET is much easier than in Visual C# .NET.
In Visual Basic .NET, as in Visual Basic 6.0, the Visual Basic compiler calls a helper method behind the scenes that uses reflection to obtain the object type. The arguments that are passed to the helper method cause the appropriate method to be invoked at run time. These arguments are the object on which to invoke the method, the name of the invoked method that is a string, and the arguments that are passed to the invoked method that is an array of objects. Additionally, you can implement late binding explicitly in code through reflection.
Imports System
Module Hello
Sub Main()
' Set up variable.
Dim helloObj As Object
' Create the object.
helloObj = new HelloWorld()
' Invoke the print method as if it was early bound
' even though it is really late bound.
helloObj.PrintHello("Visual Basic Late Bound")
End Sub
End Module
In Visual C# .NET, implementing late binding is more difficult than in Visual Basic .NET. Instead of having the compiler implement late binding, you must explicitly implement late binding in code by using reflection.
Visual C# .NET permits you to write unmanaged code. In unmanaged code, you can do things such as declare and operate on pointers, perform conversions between pointers and integral types, and take the address of variables. In a sense, writing unmanaged code is much like writing Visual C code in a Visual C# .NET program.
Because code that is written by using an unmanaged context cannot be verified to be safe, it is run only when the code is fully trusted. Do not use unmanaged context to try to write Visual C code in Visual C# .NET. Unmanaged code must be clearly marked with the modifier unsafe so that developers cannot use unmanaged features accidentally, and the execution engine works to make sure that unmanaged code cannot be run in a non-trusted environment. The scope of the unmanaged context extends from the parameter list to the end of the function, so pointers can also be used in the parameter list.
In Visual Basic .NET, you cannot write unmanaged code.
The following table lists the keywords that Visual Basic .NET and Visual C# .NET use in several categories. This information can also be found in the Visual Studio .NET online documentation.
|
|
Purpose |
Visual Basic .NET |
Visual C# .NET |
|
|
|
Object Oriented Programming |
|
|
|
|
|
Indicates a class constructor |
Public Class Class1
Public Sub New(..)
MyBase.New
…
End Sub
…
End Class
Note: You have to call the base class constructor explicitly in Visual Basic .NET. |
public class Class1
{
public Class1(..)
{
…
}
….
}
Note: The call to the base class constructor (base()) is generated automatically by the compiler in Visual C# .NET if you do not include constructor initializers. |
|
|
|
Indicates a class destructor
Note: The Destructor or Finalize method is called by garbage collection. |
Protected Overrides Sub Finalize()
m_Gadget = Nothing
m_Gear = Nothing
MyBase.Finalize()
End Sub
|
public class Class1
{
public ~Class1()
{
….
}
} |
|
|
|
Declares a class |
Class |
class |
|
|
|
Indicates class inheritance |
Public Class A
Inherits B
…
End Class |
public class A : B
{
…
} |
|
|
|
Indicates that the class can only be inherited and cannot be instantiated |
MustInherit |
abstract |
|
|
|
Indicates that the class cannot be inherited |
NotInheritable |
sealed |
|
|
|
Calls your own implementation of the method instead of an overridden method in the derived class |
MyClass |
None
|
|
|
|
Refers to a base class from the derived class |
MyBase |
base |
|
|
|
Declares a type-safe reference to a class method |
Delegate |
delegate |
|
|
|
Indicates that the method or the property overrides the implementation in its base class |
Overrides |
override |
|
|
|
Indicates that these methods have no implementation and must be implemented in derived classes |
MustOverride
(in MustInherit
class) |
abstract
(in abstract
class) |
|
|
|
Indicates that the method or the property cannot be overridden in derived classes |
NotOverridable
Note: By default, methods are not overridable. |
sealed |
|
|
|
Indicates that the method or the property can be overridden in an inheriting class |
Overridable |
virtual |
|
|
|
Overloads a procedure, a function, or a method |
Overloads |
None. Define functions with same name but different signatures. |
|
|
|
Specifies that a variable can contain an object whose events you want to handle |
WithEvents |
No specific keyword |
|
|
|
Specifies the events for which an event procedure will be called |
Handles (Event procedures can still be associated with a WithEvents variable by naming pattern.) |
n/a |
|
|
|
Evaluates an object expression one time to access multiple members |
With objExpr
<.member>
<.member>
End With |
n/a |
|
|
|
Refers to the current object |
Me |
This |
|
|
|
Declares an enumerated type |
Enum
…
End Enum |
Enum |
|
|
|
Declares an interface |
Interface |
interface |
|
|
|
Implements an interface |
Implements |
class C1 : I1 |
|
|
|
Indicates an indexer |
Default Property |
public string this[int index]
{
get {return List[index];}
set {List[index]=value;}
} |
|
|
|
Class Access Modifiers |
|
|
|
|
|
Indicates that the modifier is accessible outside the project or the assembly |
Public |
public |
|
|
|
Indicates that the modifier is accessible inside the assembly only |
Friend |
internal |
|
|
|
Indicates that the modifier is accessible only in the project (for nested classes, in the enclosing class) |
Private |
private |
|
|
|
Class Member Access Modifiers |
|
|
|
|
|
Indicates that the modifier is accessible outside the class and the project |
Public |
public |
|
|
|
Indicates that the modifier is accessible outside the class, but in the project |
Friend |
internal |
|
|
|
Indicates that the modifier is only accessible in a class or a module |
Private |
private |
|
|
|
Indicates that the modifier is accessible only to current and derived classes |
Protected |
protected |
|
|
|
Indicates the union of Protected and Friend or Internal |
Protected Friend |
protected internal |
|
|
|
Indicates that the members are shared across all instances |
Shared |
static |
|
|
|
Miscellaneous Lifetime |
|
|
|
|
|
Preserves the local variables for the procedure |
Static |
n/a |
|
|
|
Other |
|
|
|
|
Calls the Windows API |
Declare statement |
use Platform Invoke |
|
|
Indicates a comment |
‘, Rem |
//, /* */ for miltine comments,
/// for XML comments |
|
|
Indicates a constant |
Const |
Const, readonly |
|
|
|
Creates a new object |
New, CreateObject |
new |
|
|
Declares a function or a method with no return value |
Sub |
void |
|
|
Declares that an object can be modified asynchronously |
n/a |
volatile |
|
|
Declares a variable |
Private, Public, Friend, Protected, Static, Shared, Dim |
declarators (keywords include user-defined types and built-in types) |
|
|
Declares a variable explicitly |
Option Explicit |
None (All variables must be declared before use) |
|
|
Declares and raises an event |
Event, RaiseEvent |
event |
|
|
Declares a structure |
Structure
…
End Structure |
struct |
|
|
Defines a default property |
Default |
by using indexers |
|
|
Declares a null object |
Nothing |
null |
|
|
Declares a namespace |
Namespace
…
End Namespace |
Namespace
{
…
} |
|
|
Indicates namespace usage |
Imports |
using |
|
|
Retrieves a character from a string |
GetChar Function |
[ ] |
|
|
Returns the address of a function |
AddressOf (For class members, this operator returns a reference to a function in the form of a delegate instance) |
delegate |
|
|
Tests for a null object |
Obj Is Nothing |
obj == null |
|
|
Tests for a database null expression |
IsDbNull |
n/a |
|
|
Threads primitives |
SyncLock |
lock |
|
|
|
|
|
|
|
Based on your personal preference and past experience, you can use either Visual Basic .NET or Visual C# .NET to build solutions. Although differences do exist between the two languages, both languages use the .NET Framework common language runtime and are equally powerful. This document only briefly discusses the differences in syntax between Visual Basic .NET and Visual C# .NET. For more detailed information about these differences and other differences that exist between the two programming languages, see the Visual Studio .NET online help. |
posted Jan 7, 2009 10:54 AM by M Soni
Roger L. Costello - With thanks from http://www.xfront.com/REST-Web-Services.html
What is REST?REST is a term coined by Roy Fielding in his Ph.D. dissertation [1] to
describe an architecture style of networked systems. REST is an acronym
standing for Representational State Transfer.
Why is it called Representational State
Transfer?The Web is comprised of
resources. A resource is any item of interest. For example, the Boeing Aircraft
Corp may define a 747 resource. Clients may access that resource with this
URL:
http://www.boeing.com/aircraft/747
A representation of
the resource is returned (e.g., Boeing747.html). The representation places the
client application in a state. The result of the client traversing a
hyperlink in Boeing747.html is another resource is accessed. The new
representation places the client application into yet another state. Thus, the
client application changes (transfers) state with each resource
representation --> Representational State Transfer!
Here is Roy Fielding's explanation of the
meaning of Representational State Transfer:
"Representational State Transfer is intended to
evoke an image of how a well-designed Web application behaves: a network of web
pages (a virtual state-machine), where the user progresses through an
application by selecting links (state transitions), resulting in the next page
(representing the next state of the application) being transferred to the user
and rendered for their use."
Motivation for RESTThe motivation for REST was to capture the characteristics of the Web
which made the Web successful. Subsequently these characteristics are being used
to guide the evolution of the Web.
REST - An Architectural Style, Not a
StandardREST is not a standard. You will
not see the W3C putting out a REST specification. You will not see IBM or
Microsoft or Sun selling a REST developer's toolkit. Why? Because REST is just
an architectural style. You can't bottle up that style. You can only understand
it, and design your Web services in that style. (Analogous to the client-server
architectural style. There is no client-server standard.)
While REST is not a standard, it does use
standards:
- HTTP
- URL
- XML/HTML/GIF/JPEG/etc (Resource
Representations)
- text/xml, text/html, image/gif, image/jpeg, etc
(MIME Types)
The Classic REST SystemThe Web is a REST system! Many of those Web services that
you have been using these many years - book-ordering services, search services,
online dictionary services, etc - are REST-based Web services. Alas, you have
been using REST, building REST services and you didn't even know it.
REST is concerned with the "big picture" of the
Web. It does not deal with implementation details (e.g., using Java servlets or
CGI to implement a Web service). So let's look at an example of creating a Web
service from the REST "big picture" perspective.
Parts Depot Web ServicesParts Depot, Inc (fictitious company) has deployed
some web services to enable its customers to:
- get a list of parts
- get detailed information about a particular
part
- submit a Purchase Order (PO)
Let's consider how each of these services
are implemented in a RESTful fashion.
Get Parts ListThe web service makes available a URL to a parts list resource. For
example, a client would use this URL to get the parts list:
http://www.parts-depot.com/parts
Note that "how" the web
service generates the parts list is completely transparent to the client. All
the client knows is that if he/she submits the above URL then a document
containing the list of parts is returned. Since the implementation is
transparent to clients, Parts Depot is free to modify the underlying
implementation of this resource without impacting clients. This is loose
coupling.
Here's the document that the client
receives:
<?xml version="1.0"?>
<p:Parts xmlns:p="http://www.parts-depot.com"
xmlns:xlink="http://www.w3.org/1999/xlink">
<Part id="00345" xlink:href="http://www.parts-depot.com/parts/00345"/>
<Part id="00346" xlink:href="http://www.parts-depot.com/parts/00346"/>
<Part id="00347" xlink:href="http://www.parts-depot.com/parts/00347"/>
<Part id="00348" xlink:href="http://www.parts-depot.com/parts/00348"/>
</p:Parts>
[Assume that through content
negotiation the service determined that the client wants the representation as
XML (for machine-to-machine processing).] Note that the parts list has links to
get detailed info about each part. This is a key feature of REST. The client
transfers from one state to the next by examining and choosing from among the
alternative URLs in the response document.
Get Detailed Part DataThe web service makes available a URL to each part resource.
Example, here's how a client requests part 00345:
http://www.parts-depot.com/parts/00345
Here's the document that the
client receives:
<?xml version="1.0"?>
<p:Part xmlns:p="http://www.parts-depot.com"
xmlns:xlink="http://www.w3.org/1999/xlink">
<Part-ID>00345</Part-ID>
<Name>Widget-A</Name>
<Description>This part is used within the frap assembly</Description>
<Specification xlink:href="http://www.parts-depot.com/parts/00345/specification"/>
<UnitCost currency="USD">0.10</UnitCost>
<Quantity>10</Quantity>
</p:Part>
Again observe how this data
is linked to still more data - the specification for this part may be found by
traversing the hyperlink. Each response document allows the client to drill down
to get more detailed information.
Submit POThe
web service makes available a URL to submit a PO. The client creates a PO
instance document which conforms to the PO schema that Parts Depot has designed
(and publicized in a WSDL document). The client submits PO.xml as the payload of
an HTTP POST.
The PO service responds to the HTTP POST
with a URL to the submitted PO. Thus, the client can retrieve the PO any time
thereafter (to update/edit it). The PO has become a piece of information which
is shared between the client and the server. The shared information (PO) is
given an address (URL) by the server and is exposed as a Web service.
Logical URLs versus Physical URLsA resource is a conceptual entity. A representation is a
concrete manifestation of the resource. This URL:
http://www.parts-depot.com/parts/00345
is a logical URL, not a
physical URL. Thus, there doesn't need to be, for example, a static HTML page
for each part. In fact, if there were a million parts then a million static HTML
pages would not be a very attractive design.
[Implementation detail: Parts Depot could
implement the service that gets detailed data about a particular part by
employing a Java Servlet which parses the string after the host name, uses the
part number to query the parts database, formulate the query results as XML, and
then return the XML as the payload of the HTTP response.]
As a matter of style URLs should not reveal the
implementation technique used. You need to be free to change your implementation
without impacting clients or having misleading URLs.
REST Web Services CharacteristicsHere are the characteristics of REST:
- Client-Server: a pull-based interaction style:
consuming components pull representations.
- Stateless: each request from client to server must
contain all the information necessary to understand the request, and cannot take
advantage of any stored context on the server.
- Cache: to improve network efficiency responses
must be capable of being labeled as cacheable or non-cacheable.
- Uniform interface: all resources are accessed with
a generic interface (e.g., HTTP GET, POST, PUT, DELETE).
- Named resources - the system is comprised of
resources which are named using a URL.
- Interconnected resource representations - the
representations of the resources are interconnected using URLs, thereby enabling
a client to progress from one state to another.
- Layered components - intermediaries, such as proxy
servers, cache servers, gateways, etc, can be inserted between clients and
resources to support performance, security, etc.
Principles of REST Web Service
Design1. The key to creating Web Services
in a REST network (i.e., the Web) is to identify all of the conceptual entities
that you wish to expose as services. Above we saw some examples of resources:
parts list, detailed part data, purchase order.
2. Create a URL to each resource. The resources
should be nouns, not verbs. For example, do not use this:
http://www.parts-depot.com/parts/getPart?id=00345
Note the verb, getPart.
Instead, use a noun:
http://www.parts-depot.com/parts/00345
3. Categorize
your resources according to whether clients can just receive a representation of
the resource, or whether clients can modify (add to) the resource. For the
former, make those resources accessible using an HTTP GET. For the later, make
those resources accessible using HTTP POST, PUT, and/or DELETE.
4. All resources accessible via HTTP GET should
be side-effect free. That is, the resource should just return a representation
of the resource. Invoking the resource should not result in modifying the
resource.
5. No man/woman is an island. Likewise, no
representation should be an island. In other words, put hyperlinks within
resource representations to enable clients to drill down for more information,
and/or to obtain related information.
6. Design to reveal data gradually. Don't
reveal everything in a single response document. Provide hyperlinks to obtain
more details.
7. Specify the format of response data using a
schema (DTD, W3C Schema, RelaxNG, or Schematron). For those services that
require a POST or PUT to it, also provide a schema to specify the format of the
response.
8. Describe how your services are to be invoked
using either a WSDL document, or simply an HTML document.
SummaryThis
article described REST as an architectural style. In fact, it's the
architectural style of the Web. REST describes what makes the Web work well.
Adhering to the REST principles will make your services work well in the context
of the Web.
In a future article I will write about the
evolution of the Web using the REST principles.
AcknowledgementThanks to Robert Leftwich and Philip Eskelin for their very helpful
comments in creating this document.
References
[1] http://www.ebuilt.com/fielding/pubs/dissertation/top.htm
|
posted Dec 29, 2008 8:29 AM by M Soni
[
updated Dec 29, 2008 9:03 AM
]
Critical stages and Processes of a project form its inception to its end : - Visualization
- Identifying
- Defining
- Initiating
- Planning
- Executing
- Controlling
- Closing
|
|