Thursday, 29 May 2014

Difference between IEnumerable VS IQueryable

IEnumerable VS IQueryable

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by Iqueryable, hence it has all the features of it and except this, it has its own features. Both have its own importance to query data and data manipulation. Let’s see both the fetures and take the advantage of both the fetures to boost your LINQ Query performance.
IEnumerable
1.       IEnumerable exists in System.Collections Namespace.
2.       IEnumerable can move forward only over a collection, it can’t move backward and between the items.
3.       IEnumerable is best to query data from in-memory collections like List, Array etc.
4.       While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
5.       IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
6.       IEnumerable supports deferred execution.
7.       IEnumerable doesn’t supports custom query.
8.       IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
9.       Extension methods supports by IEnumerable takes functional objects.
IEnumerable Example
1.   MyDataContext dc = new MyDataContext ();
2.  IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
3.  list = list.Take<Employee>(10);
Generated SQL statements of above query will be :
1.   SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
2.  WHERE [t0].[EmpName] LIKE @p0
Notice that in this query "top 10" is missing since IEnumerable filters records on client side
IQueryable
1.       IQueryable exists in System.Linq Namespace.
2.       IQueryable can move forward only over a collection, it can’t move backward and between the items.
3.       IQueryable is best to query data from out-memory (like remote database, service) collections.
4.       While query data from database, IQueryable execute select query on server side with all filters.
5.       IQueryable is suitable for LINQ to SQL queries.
6.       IQueryable supports deferred execution.
7.       IQueryable supports custom query using CreateQuery and Execute methods.
8.       IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
9.       Extension methods supports by IEnumerable takes expression objects means expression tree.
IQueryable Example
1.   MyDataContext dc = new MyDataContext ();
2.  IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
3.  list = list.Take<Employee>(10);
Generated SQL statements of above query will be :
1.   SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
2.  WHERE [t0].[EmpName] LIKE @p0

Notice that in this query "top 10" is exist since IQueryable executes query in SQL server with all filters.

Wednesday, 21 May 2014

How to Call a Service Method Asynchronously using WCF

How to Call a Service Method Asynchronously using WCF

Most methods in Windows Communication Foundation (WCF) services may be called either synchronously or asynchronously. Calling a method asynchronously enables your application to continue to work while the method is being called when it operates over a slow connection.
By default, when a service reference is added to a project it is configured to call methods synchronously. You can change the behavior to call methods asynchronously by changing a setting in the Configure Service Reference dialog box.


 Note
This option is set on a per-service basis. If one method for a service is called asynchronously, all methods must be called asynchronously.
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements.


To call a service method asynchronously
  1. In Solution Explorer, select the service reference.
  2. On the Project menu, click Configure Service Reference.
  3. In the Configure Service Reference dialog box, select the Generate asynchronous operations check box.


How to find the available endpoints for a WCF service

    How to find the available endpoints for a WCF service

 

1.       In Solution Explorer, right-click the app.config file for the project that contains the service reference and then click Open. The file will appear in the Code Editor.

2.       Search for the <Client> tag in the file.

3.       Search underneath the <Client> tag for a tag that starts with <Endpoint>.
If the service reference provides multiple endpoints, there will be two or more <Endpoint tags.

4.       Inside the <EndPoint> tag you will find a name="SomeService" parameter (where SomeService represents an endpoint name). This is the name for the endpoint that can be passed to the endpointConfigurationName As String overload of a constructor for a service reference.


How to select a service endpoint in WCF

    How to select a service endpoint in WCF

 

1.       Add a reference to a WCF service. For more information

2.       In the Code Editor, add a constructor for the service reference:
 
ServiceReference.Service1Client proxy = new ServiceReference.Service1Client()
 
 
NoteNote
Replace ServiceReference with the namespace for the service reference and replace Service1Client with the name of the service.
 
 
 
3.       An IntelliSense list will be displayed with the overloads for the constructor. Select the endpointConfigurationName As String overload.
4.       Following the overload, type = ConfigurationName, where ConfigurationName is the name of the endpoint that you want to use.


Note
If you do not know the names of the available endpoints, you can find them in the app.config file.

Windows Communication Foundation Services and WCF Data Services in Visual Studio

Windows Communication Foundation Services and WCF Data Services in Visual Studio


Visual Studio 2008 provides tools for working with Windows Communication Foundation (WCF) and WCF Data Services, Microsoft technologies for creating distributed applications. This topic provides an introduction to services from a Visual Studio perspective.


Windows Communication Foundation (WCF) is a unified framework for creating secure, reliable, transacted, and interoperable distributed applications. In earlier versions of Visual Studio, there were several technologies that could be used for communicating between applications.
If you wanted to share information in a way that enabled it to be accessed from any platform, you would use a Web service (also known as an ASMX Web service). If you wanted to just move data between a client and server that was running on the Windows operating system, you would use .NET Remoting. If you wanted transacted communications, you would use Enterprise Services (DCOM), or if you wanted a queued model you would use Message Queuing (also known as MSMQ).
WCF brings together the functionality of all those technologies under a unified programming model. This simplifies the experience of developing distributed applications.


WCF Data Services are services that interact directly with a database, allowing you to return data using standard HTTP verbs such as GET, POST, PUT or DELETE. In general, WCF Data Services are a good choice for applications that are used to create, update, or delete records in a database. For more information, see ADO.NET Data Services Framework.


The WCF Programming model is based on communication between two entities: a WCF service and a WCF client. The programming model is encapsulated in the System.ServiceModel namespace in the .NET Framework.

A WCF service is based on an interface that defines a contract between the service and the client. It is marked with a ServiceContractAttribute attribute, as shown in the following code:

C#
 [ServiceContract]
public interface IService1
C#
 [OperationContract]
string GetData(string value);
You define functions or methods that are exposed by a WCF service by marking them with a OperationContractAttribute attribute. In addition, you can expose serialized data by marking a composite type with a DataContractAttribute attribute. This enables data binding in a client.
After an interface and its methods are defined, they are encapsulated in a class that implements the interface. A single WCF service class can implement multiple service contracts.
A WCF service is exposed for consumption through what is known as an endpoint. The endpoint provides the only way to communicate with the service; you cannot access the service through a direct reference as you would with other classes.
An endpoint consists of an address, a binding, and a contract. The address defines where the service is located; this could be a URL, an FTP address, or a network or local path. A binding defines the way that you communicate with the service. WCF bindings provide a versatile model for specifying a protocol such as HTTP or FTP, a security mechanism such as Windows Authentication or user names and passwords, and much more. A contract includes the operations that are exposed by the WCF service class.
Multiple endpoints can be exposed for a single WCF service. This enables different clients to communicate with the same service in different ways. For example, a banking service might provide one endpoint for employees and another for external customers, each using a different address, binding, and/or contract.

A WCF client consists of a proxy that enables an application to communicate with a WCF service, and an endpoint that matches an endpoint defined for the service. The proxy is generated on the client side in the app.config file and includes information about the types and methods that are exposed by the service. For services that expose multiple endpoints, the client can select the one that best fits its needs, for example, to communicate over HTTP and use Windows Authentication.
After a WCF client has been created, you reference the service in your code just as you would any other object. For example, to call the GetData method shown earlier, you would write code that resembles the following:

C#
private void button1_Click(System.Object sender, System.EventArgs e)
{
    ServiceReference1.Service1Client client = new
        ServiceReference1.Service1Client();
    string returnString;

    returnString = client.GetData(textBox1.Text);
    label1.Text = returnString;
}


Tuesday, 20 May 2014

JQuery Comparison and Logical operators


Comparison and Logical operators are used to test for true or false.

Comparison Operators
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x=5, the table below explains the comparison operators:

Operator
Description
Comparing
Returns
==
equal to
x == 8
false
x == 5
true
===
exactly equal to (equal value and equal type)
x === "5"
false
x === 5
true
!=
 not equal
x != 8
true
!==
 not equal (different value or different type)
x !== "5"
true
x !== 5
false
> 
 greater than
x > 8
false
< 
 less than
x < 8
true
>=
 greater than or equal to
x >= 8
false
<=
 less than or equal to
x <= 8
true



How can it be used
Comparison operators can be used in conditional statements to compare values and take action depending on the result:
if (age < 18) text = "Too young";
You will learn more about the use of conditional statements in the next chapter of this tutorial.

Logical Operators
Logical operators are used to determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:

Operator
Description
Example
&&
and
(x < 10 && y > 1) is true
||
or
(x == 5 || y == 5) is false
!
not
!(x == y) is true



Conditional Operator
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
Syntax
variablename=(condition)?value1:value2 
Example
If the variable age is a value below 18, the value of the variable voteable will be "Too young", otherwise the value ofvoteable will be "Old enough":

voteable = (age < 18) ? "Too young":"Old enough";