Thursday, March 30, 2017

Difference between REST and SOAP

Difference between REST and SOAP 

Here are some fundamental differences between REST, RESTful and SOAP Web Services, which will help you not only to understand these two key technologies better but also to answer some tricky Java web services questions based upon these two technologies :
Short Form
REST stands for REpresntational State Transfer (REST) while SOAP Stands for Simple Object Access Protocol (SOAP).


Architecture style vs Protocol
REST is an architectural style, on which RESTFul web services are built while SOAP is a standard devised to streamline communication between client and server in terms of format, structure and method.


Use of HTTP Protocol
REST takes full advantage of HTTP protocol, including methods e.g. GET, POST, PUT, and DELETE to represent action e.g. from an application which provides data related to books, GET request can be used to retrieve books, POST can be used to upload data of a new book, and DELETE can be used to remove a book from library. On the other hand, SOAP uses XML messages to communicate with the server.


Supported Format
RESTful web service can return the response in various format e.g. JSON, XML and HTML, while by using SOAP web service you tie your response with XML because actual response is bundled inside a SOAP message which is always in XML format.


Speed
Processing a RESTful web service request is much faster than processing a SOAP message because you need to less parsing. Because of this reason RESTful, web services are faster than SOAP web service.


Bandwidth
SOAP messages consume more bandwidth than RESTFul messages for the same type of operation because XML is more verbose than JSON, standard way to send RESTFul messages and SOAP has an additional header for every message while RESTFul services utilize HTTP header.


Transport Independence
Since SOAP messages are wrapped in a SOAP envelope it can be sent over to any transport mechanism e.g. TCP, FTP, SMTP or any other protocol. On the other hand, RESTful Web services are heavily dependent upon HTTP protocol. They used HTTP commands their operation and depends upon on HTTP for transmitting content to the server. Though in a real world, SOAP is mostly over HTTP so this advantage of transport independence is not really utilized.


Resource Identification
RESTful web services utilize URL to identify the desired resources to be accessed while SOAP uses XML messages to identify the desired web procedure or resource to be invoked.


Security
Security in RESTful web service can be implemented using standard and traditional solutions for authorized access to certain web resources. While to implement security in SOAP based web services you need additional infrastructure in the web to enable message or transport level security concerns.


Caching
RESTful web service takes full advantage of the web caching mechanism because they are basically URL based. On the other hand, SOAP web services totally ignore web caching mechanism.


Approach
In REST based web-services every entity is centered around resources, while in the case of SOAP web service, every entity is centered around interfaces and messages.


An Example
In the first paragraph, we have seen one example of requesting same web service using both SOAP and RESTFul style, you can see that REST web service is easy to understand, can be cached and required little effort to understand as compared to SOAP.



Summary - RESTful vs SOAP Web Service

Here is a nice summary of key differences between REST and SOAP style web services :



Some Technical details about SOAP Web Services

1) SOAP stands for Simple Object Access Protocol but nothing is so simple about it :)

2) It's only possible to send XML messages to the server using SOAP web service because your request is embedded inside SOAP envelope which is in XML format, on the other hand, RESTful web services allow you to send a request in various formats e.g. JSON and flexible enough to send JSON responses.



Why REST is better than SOAP?

Now that you know some differences between REST and SOAP web services, let's summarize our reasons of why REST is better choice for modern day web service requirement :

1. REST can be consumed by any client  e.g. Java, C++, Python client and even a web browser with Ajax and JavaScript.

2. REST is lightweight as compared to SOAP, it doesn't require CPU consuming XML parsing and it also consumes less bandwidth because unlike SOAP, REST doesn't require a SOAP header for every message.

3. SOAP is an old technology, all modern technical giant are using REST e.g. Google, Twitter, and Flickr.

4. REST is easy to learn, its just nouns and verbs. If you already know HTTP methods then its even easier.

5. Java has excellent support for RESTFul web services, well it also has good support for SOAP web services but you have lots of choices here e.g. Jersey, RESTLet etc.


That's all about the difference between REST and SOAP Web Service in Java. Its's one of the most frequently asked questions on Java web service topic. Since REST is the technology which is right now dominating web service space, it's also important to know the pros and cons REST style of web service provides over good old secure SOAP web services.


Read more: http://javarevisited.blogspot.com/2015/08/difference-between-soap-and-restfull-webservice-java.html#ixzz4coOoObxX

LINQ Interview Questions & Answers

http://www.dotnetfunda.com/interviews/cat/110/linq/

what is json

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language


What is a JSON string?

JavaScript Object Notation (JSON) is a lightweight data-interchange format inspired by the object literals of JavaScript. JSON values can consist of: objects (collections of name-value pairs), arrays (ordered lists of values) strings, (in double quotes), numbers true, false, or null.
What is serialization in JSON?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation. When transmitting data or storing them in a file, the data are required to be byte strings, but complex objects are seldom in this format.

for more click here https://www.w3schools.com/js/js_json_intro.asp

Wednesday, March 29, 2017

Comparison between List, IList and IEnumerable

  • IEnumerable<T> is the base interface that the following extend or implement. It doesn't allow for direct access and is readonly. So use this only if you intend to iterate over the collection.
  • ICollection<T> extendsIEnumerable<T> but in addition allows for adding, removing, testing whether an element is present in the collection and getting the total number of elements. It doesn't allow for directly accessing an element by index. That would be an O(n) operation as you need to start iterating over it until you find the corresponding element.
  • IList<T> extends ICollection<T> (and thus it inherits all its properties) but in addition allows for directly accessing elements by index. It's an O(1) operation.
  • List<T> is just a concrete implementation of the IList<T> interface.
IEnumerable is the base of the ICollection and IList interfaces

public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface ICollection : IEnumerable
{
int count { get; }
bool IsSynchronized { get; }
Object SyncRoot { get; }
void CopyTo(Array array,int index);
}
public interface ICollection : IEnumerable, IEnumerable
{
int count { get; }
bool IsReadOnly { get; }
void Add(T item);
void Clear();
bool Contains(T item);
void CopyTo(T[] array, it arrayIndex);
bool Remove(T item);
}
public interface IList : ICollection,IEnumerable
{
bool IsFixedSize { get; }
bool IsReadOnly { get; }
Object this[int index] { get;set; }
int Add(Object value);
int IndexOf(Object value);
void Insert(intindex,object value);
void Remove(Object value);
void RemoveAt(int index);
}

When to Use?

Now since we know all interfaces, the next question we have is when we have to use which interface?
The important point is to use the interface that our application needs us to use.
InterfaceBest Practices
IEnumerableIEnumerable<T>The only thing you want is to iterate over the elements in a collection.
You only need read-only access to that collection.
ICollectionICollection<T>You want to modify the collection or you care about its size.
IListIList<T>You want to modify the collection and you care about the ordering and / or positioning of the elements in the collection.
ListList<T>As per DIP, you should depend on abstractions instead of implementations, you should never have a member of your own implementations with the concrete type List/List.

Virtual vs Override vs New Keywords in C#

Virtual Keyword

Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding. Virtual keyword is used within a set with override keyword. It is used as:
Hide   Copy Code
// Base Class
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }

Override Keyword

Override keyword is used in the derived class of the base class in order to override the base class method. Override keyword is used with virtual keyword, as:
Hide   Copy Code
// Base Class
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
// Derived Class
 
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }

New Keyword

New keyword is also used in polymorphism concept, but in the case of method overloading So what does overloading means, in simple words we can say procedure of hiding your base class through your derived class.
It is implemented as:
Hide   Copy Code
class A
    {
        public void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public new void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }

Demo Example

Sample Implementation

Here’s a simple implementation in C# without using any keyword. Do you think it will run fine or show some RUN or COMPILE time errors?
Let’s see:
Hide   Shrink Description: https://www.codeproject.com/images/arrow-up-16.png   Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
    class A
    {
        public void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }
 
    class Polymorphism
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show(); 
            A a2 = new B();
            a2.show();
 
        }
    }
}

Output Window

Description: https://www.codeproject.com/KB/cs/816448/image001.jpg
It is showing some sort of output which means there is neither run time nor compile time error. But it will definitely show a Warning to you in your Visual Studio. So what is it and how to remove it. Do you want to know?
Keep reading and you will go through that.

Warning Message

Here’s a warning message that you will get:
Description: https://www.codeproject.com/KB/cs/816448/image002.jpg

Solution

The solution of this problem is already in the warning. Just read it carefully and you will get that. Yes, you got that right, for removing that warning we need to use NEW keyword.
In the next sample, the demo example shows you a simple demo snippet and implementation of new keyword.
(I hope now you get why we are using new keyword in here.)

New Keyword | Method Overloading

Here’s a simple snippet of method overloading mechanism. So just go through it and guess the output:
Hide   Shrink Description: https://www.codeproject.com/images/arrow-up-16.png   Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
    class A
    {
        public void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public new void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }
 
    class Polymorphism
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show(); 
            A a2 = new B();
            a2.show();
 
        }
    }
}

Output Window

Description: https://www.codeproject.com/KB/cs/816448/image001.jpg

Explanation

The procedure goes something like this:
(using overhiding as a ref for overloading)
Description: https://www.codeproject.com/KB/cs/816448/image003.gif

Virtual & Override Keywords | Method Overriding

This is a simple example of method overriding. Just go through it and guess the output again and also try to differentiate between the previous snippet and this snippet.
Hide   Shrink Description: https://www.codeproject.com/images/arrow-up-16.png   Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }
 
    class Polymorphism
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show();
            A a2 = new B();
            a2.show();
        }
    }
}

Output Window

Description: https://www.codeproject.com/KB/cs/816448/image004.jpg

Explanation

The flow goes something like this:
Description: https://www.codeproject.com/KB/cs/816448/image005.gif

Overriding + Hiding | Together

In this snippet, I show how both these methods can work together in the same snippet. So just go through this and guess the output.
Hide   Shrink Description: https://www.codeproject.com/images/arrow-up-16.png   Copy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Generics
{
    class A
    {
        public virtual void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public override void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }
 
    class C : B
    {
        public new void show()
        {
            Console.WriteLine("Am Here!");
            Console.ReadLine();
        }
    }
 
    class Polymorphism
    {
        public static void Main()
        {
            A a1 = new A();
            a1.show();
            B b1 = new B();
            b1.show();
            C c1 = new C();
            c1.show();
            A a2 = new B();
            a2.show();
            A a3 = new C();
            a3.show();
            B b3 = new C();
            b3.show();
        }
    }
}

Output Window

Description: https://www.codeproject.com/KB/cs/816448/image006.jpg

Explanation

The flow goes something like this:
(using overhiding as a ref for overloading)
Description: https://www.codeproject.com/KB/cs/816448/image007.gif

Key Points

I am giving some key points about these keywords by taking reference of method overloading and overriding concepts, as these keywords are used in these mechanism.

Virtual & Override

·         Used in polymorphism implementation
·         Includes same method name and same params’
·         Used in method overriding concept
·         It is also called run time polymorphism
·         Causes late binding

New

·         It is also used in polymorphism concept
·         Includes same method name and different params’
·         Used in method overloading concept
·         It is compile time polymorphism
·         Cause early binding