Sunday, September 4, 2011

Dance with LINQ

For the first time I saw LINQ syntax I told to myself what the...
How in the world I can understand and write LINQ syntax easily. What is LINQ in first place? Is there any value for me to learn this. Guess what I started to think in ways to use it. Then I found it very handy.
In next posts I will try to teach you LINQ how to use it and how to understand exactly what are you doing and what LINQ can offer you.
Let's start with what is LINQ. after all if you want to start dancing with it you need to know your partner otherwise your dance will not be that much sexy! eventhough you are a good dancer.

LINQ is microsoft solution to query different sources like objects, XML, Dataset, SQL and entities easily with less code. However it is not just to query data. You can use LINQ to iterate items and even change thier types and use it to sort and etc.

at this stage please do not worry about the code in future posts I will talk about C# 3.0 futures so you will understand exactly how LINQ works. in the following example a list of string numbers are changing to int and also they are get sorted. I am sure you see what a nice functionality it it.

string[] mynums = {"34","020","3","16"};
int[] nums = mynums.Select(s=> Int32.Parse(s)).OrderBy(s=>s).ToArray();
foreach (int n in nums)
Console.WriteLine(n);

In next couple of posts I will talk about C# enhancements for LINQ so when we start LINQ then you have a better idea what is going on.

Hopefully you liked the first dance class with LINQ.

Please keep me updated.

Friday, February 20, 2009

WCF Contracts

A contract is at name implies is a standard way of describing what a service can do. Meanwhile, it is platform independent. In WCF we have 4 types of contract:
· Service Contract: It defines what a client can do with the service.
· Data Contract: It defines which data types can be transferred in the service. ( by default you can pass basic types like int, string however for custom types like a custom class you need to define the class as a Data Contract to be able to pass object of that class in the service)
· Fault Contract: It defines what are the errors raised by service.
· Message Contract: It is an extra functionality allows you to send message in a customized way (It means that you need to send a message in a specific way because let’s say there is an existing system that just understands messages in a specific way. As a user of WCF you should not usually use this as you do not need it except you are working with very specific system that needs specific messaging system)

Service Contract:
To allow a WCF Client to understand what they can do with the service you need to use Service Contract which is an attribute you need to add to the class. Also for each method that you wish WCF client to access you need add OperationContract attribute take a look at this sample

[ServiceContract]
Class MyService
{
[OperationContract]
public string MyOperation()
{
return “hello World”;
}
}

As you see in above example we have ServiceContract for the class and we have OperationContract for the methods. Some Points:
· You can apply ServiceContract to a class or interface
· You have to apply OperatoinContract for methods otherwise it will not be accessible to clients
· You can only apply OperationContract to methods
· It is highly recommended first you better create an interface and apply the servicecontract then create a class that implements the interface (the reason will be discussed further)
· Methods accessibility is not important to WCF (public, private or internal) since they are CLR concepts not WCF
· Always the default constructor will be called (avoid parameterized constructors)
· You can define a Namespace for the ServiceContract (and it is suggested to do so) to avoid collision
Please see the same sample by using interface and namespace
[ServiceContract(Namespace=”mycompany.com”)]
interface IMyService
{
[OperationContract]
string MyOperation();
}
Class MyService:IMyService
{
public string MyOperation()
{
return “hello World”;
}
}