Tuesday, June 2, 2009

Delegates & Events


This time I am trying to expound one of the most controversial and debated topics of .NET with a hope in the heart that it makes a sense to the readers. I honestly welcome any critics or suggestions needed for the improvement of this article.
What are the Delegates?
Delegates are nothing but array of addresses of the methods. The word “Array of Addresses” is used here because a delegate can contain addresses of several methods.But the delegates are type safe; as a result a delegate can contain the addresses of a similar type of methods.
Declaring a Delegate
The delegate is declared in the following way
private delegate Method Return Type myDelegate (Method Parameters);
More specifically suppose I want to declare a delegate for the method
private int myMethod1 (int x, int y);
it can be declared as follows
private delegate int myDelegate (int x, int y);
Assigning method to the delegates
The method myMethod1 can be assigned to the "myDelegate" in the following way.
myDelegate=myMethod1;
Now suppose there are following methods which have got similar signature and return types.
private int myMethod2 (int x, int y);
private int myMethod3 (int x, int y);
private int myMethod4 (int x, int y);
Then these methods can also be added in the "myDelegate" in the following way:
myDelegate+=myMethod2;
myDelegate+=myMethod3;
myDelegate+=myMethod4;
For the sake of more clarification let's assume that methods myMethod1, myMethod2, myMethod3, myMethod4 are stored at the locations 100,200,300,400 respectively then in this case our delegate "myDelegate" will be an array consisting of the elements 100,200,300,400.
This means the delegate “myDelegate” can call the all those methods when it is invoked.’A method can be removed from the delegate in the following waymyDelegate-=myMethod4;
Complier representation of Delegates
When we declare a delegate like

private delegate int myDelegate ();

The .NET generates the following class for this.

internal delegate int myDelegate(string someValue)
{
public myDelegate(object object, IntPtr method);
public virtual IAsyncResult BeginInvoke(string someValue,AsyncCallback callback, object object);
public virtual int EndInvoke(IAsyncResult result); public override int Invoke(string someValue);
}

Saturday, May 30, 2009

.Net 3.5 New feature - Entity Framework

Why something like Entity Framework was needed?
Before the release of .NET 3.5, there was a substantial mismatch between the relation database model and the business entity model. Let’s take an example to understand it in a better way.We want to develop an application for a hospital with the following set of entities.
  • Users: The users who will be logging into the system.
  • Roles: The role of the users.
  • Donors: Some of the users may be donors as well.

I have prepared the following tables which contain information about the Users, Roles and Donors.



Now suppose our business need tells us to display the name and detail of the doctors who are also donors. Then I can think of writing the following query:
Select Users.User_Id, Roles.Role_Name, Users.First_Name, Users.Second_Name, Users.Middle_Name, Users.Last_name from Users INNER JOIN Roles ON User.Role_Id = Roles.Role_Id INNER JOIN Donors on Donors.User_Id=User.User_Id WHERE Role.Role_Name=’Doctor’
I can see two major issues here.

  • We need complex queries (although it’s not that much complex here) with too many joins to populate the business entities.
  • If we have the business entities like Roles, Users, Donors, it will not have any business significance until we populate these with this kind of complex queries.

These issues can be dealt in a better way with the help of Entity Framework.
Prerequisites for using Entity Framework

  • Visual Studio 2008 with SP-1
  • Net Framework 3.5 with SP-1
  • SQL Server 2005

Getting started with Entity Framework
Open your solution in the VS2008, click on add new item and add a new ADO.NET entity model.

Provide the name of the entity data model say "NICRModel" and click on Add.The visual studio shall show the following screen.

Now select "Generate from the database" and click on the Next button.In this screen provide the SQL DB settings.


Clicking on the Next button brings up the following screen where in you can select the tables for which the entity data model has to be created.



Click on finish button.You will be shown the following entity model diagram.


As you can see, the diagram shows the business entities with their respective relations.

Now consider the above scenario , we have to find out the details of the doctors who are also donors.

In that case you will have to create an object of the entity model..
NICREntities NicrEntity = new NICREntities();

Now find out the desired information using LINQ
IQueryable UsersQuery = from p in NicrEntity.Users
where p.Roles.Role_Description == "Doctors" && p.Donor.Count>0
select p;

You can cleary see out from here that the much of our effort is simplified by the Entity Framework concept.