Thursday, May 21, 2009

OOP Concepts

I know every programmers know the OOP concept but still I have decided to write small and sweet article related to OOP used to refresh our OOP concepts.

Definition:

OOP is the programming model where related information (data/fields-properties and processes/method) are integrated to form an object.

Advantages:

- Code redundancy and maintenance is lower.
- Code readability (Abstraction), reusability (Inheritance), reliability, security (Encapsulation) is higher.
- Less coding
- In OOP, everything (data and process) is related to object and object is closely related to real life.

Disadvantages:

- Performance/efficiency is lower then Structured Programming

Reason:
1. Object is reference type and referenced through pointer. So little bit of space overhead to store pointers and little bit of speed overhead to find the space in the memory (heap) to store the objects runtime.
2. For dynamic methods there is little bit of time overhead to call the right method while OOP features used like inheritance, polymorphism.

Features:
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism

Abstraction:

A concept without thinking/showing of a specific example/actual implementation. It is the process to hide/omit unwanted detail and showing just representation of the actual implementation/detail.

Abstraction refers to the act of representing essential features without including the background details or explanations.

e.g. Real time examples are,
- Stopping the car engine, starting the car engine, switch on/off to any electrical devices.

Abstraction is achieved by classes and objects.

Encapsulation:

It is the process of integrating the data and functions into a single unit called class.
The objective of encapsulation is to provide a).Protection and b).Information Hiding.

a).Protection is the limiting and use of class’ data and functions. In short, data and/or functions can not be used with out object. Protection is about adding methods and data to a class. When you add methods or data to a class, then you are protecting the methods or data from use without first having an object of the class.

b).Information hiding is the removing the data and functions from public space of class. It can be achieved using access modifiers like private, public, protected.

Abstraction tells us what external face we should present to the world where as Encapsulation ensures that the implementation of the interface doesn’t leak out to the outside world.

Inheritance:

It is the process to inherit/derive commonly used data (state) and functions (behavior) from other classes, called parent/base class and inherited/derived class called child or derived class. It is intended to reuse existing code with little or no modification.

a).Generalization and b). specialization are the associated with the concept of inheritance.

a). Generalization is the defining the classes, called base/parent/super class, having common characteristics of its subclasses.
e.g. “Vehicle” is the generalization of “car”, ”bike”, ”plane”, ”boat”, ”bicycle” and many more.

b).Specialization is to refine the derived/child class behavior using overriding.
e.g. Start and Stop behaviors of vehicle are different. Car has different mechanism to start then bike.

Polymorphism:

Poly – multi and Morph =”forms” -> having multiple forms
Polymorphism is the process of using an operator or function in different ways for different set of inputs given.

Redefining the inherited the members differently using overloading and overriding is called polymorphism

There are 3 concepts to achieve polymorphism;

1. Function overloading – functions having same name but different signatures/prototype does not include return type
e.g. Add(int a, int b) - Adds two integer numbers
Add(string a, string b) - concatenates the string a & b
2. Operator overloading – redefining existing operator for the objects of the classes.
e.g. + used to add numbers
+ used to
concatenates two string
3. Function overriding - redefining the derived method into the derived class.
e.g. Work() - defined in Parent class - Parent do business
Work() - defined in Child class - Child does the job

Tuesday, May 19, 2009

Facebook - Linq to Fql

Step 1: Requirements

1. Download the Facebook LINQ to FQL binaries(DLLs) from http://facebooklinq.codeplex.com/
2. Create a website or application in VS 2008
3. Add references of DLL (downloaded in step1) in to the project.

Step 2: Facebook Application Setting

4. Login in http://www.Facebook.com with valid user name and password.
5. Paste the URL http://www.facebook.com/developers/ to support custom application in your account.
6. Click “Allow” button to allow for custom Facebook application
7. Click “Setup New Application” button to validate your custom application
8. Give the application name – let’s say “My Application” and select “Agree” radio button then click “Save Changes” button.
8. Go to "Canvas" tab and enter the "Canvas Callback URL". Let's say "http://localhost:1111/FBDemo/Default.aspx
9. On the same tab, select Render Method as IFrame from "Canvas Setting" section.
10. Facebook generates some credentials for the created application like “API Key”, “Application Secret”,” Application ID” with respective values. See below fig


Step 3: Setting the Facebook application credentials in Web.Config

11. Add below values in “appSettings” section

12.Add below value in “httpHandlers” section

Step 4: Accessing Facebook Data

13. Open the webpage (in our case Default.aspx)
14. Add reference of Facebook dlls using below lines
using facebook;
15. Authenticate current Facebook Logged in user with below line
bool isAuthenticated = facebook.Web.FacebookContext.Current.TryAuthenticating(true);
16. After authentication, create the object of FacebookDataContext with below line
var db = new FacebookDataContext();
17. Now access the properties of db whose type of facebook.Linq.FqlTable indicates the facebook
data like;
a. db.user – user information
b. db.group – groups information
c. db.friend_info – friends list etc.
18. Filter data as per requirements through executing LINQ on the above lists

Step 5: Source code listing
a. ASPX Page
Put the DataList and add item templates in to the datalist as per requirements

b. Source Listing (C# Code):
Write the below code to display the logged in user information like name, birthday and photo of the user.
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (facebook.Web.FacebookContext.Current.TryAuthenticating(true))
{
var db = new FacebookDataContext();
var myUser = from user in db.user where user.uid == db.uid select user;
var myUser2 = db.user.Where(t => t.uid == db.uid);
Example1DataList.DataSource = myUser.Take(10);
DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
}