Sunday, May 11, 2008

Multi tier programming –Part1

see these Related topics:

  1. Multi tier basics
  2. A best practice for multi tier architecture
  3. A sample code based on best practice
  4. Examine utilities to speed up Multi tier programming- NLog
First of all having a good understanding of multi tier programming is really important. Then we need to look into how we can implement it. What is a good practice especially for web applications? Unfortunately Multi tier programming needs massive time in terms of implementation so how we can utilize speeding at the same time write our code in this architecture?

Let’s start with what is multi tier programming?
Multi tier programming suggests that instead of writing code in just UI we need to separate our applications code to various layers. What does layer mean? Each layer provide some classes that having a unique purpose like one layer is just to check your business rule or do complex calculation to satisfy your business. Another layer can have responsibility for saving and retrieving data. Be careful we do not suggesting that these layers should be in different computers all we suggest is that we need to separate our application code to different layers logically. Let’s understand this concept in one example:

We need to save and show information of some customers. Customers’ data exists in an Access database. We are asked to create a web application to show these data. So we create two pages one page to save new customer information another for showing all customer data. To implement each page usually we write some code in code behind of that page to save or retrieve data then we bind to database. This is wrong! I know that some of us have been doing this for years and everything was fine but I will convince you that is absolutely wrong. We get back to this issue that why this is wrong. Multi tier programming suggests that we can have one class to save and retrieve from data base (which can be our data access) another class to work with previous class and apply possible business rules and finally in UI to save and load information, object of business class should be created. It seems the code that could be very simple is more complicated. Yeah this is true it is a little bit more complicated however, it is definitely worth it. Why?

Reasons of using multi tier programming:

It helps you to separate each layer purpose so finding a code will be much easier. You can replace each layer let’s say in last example what if you need to change your database to SQL server then you need just replace data access layer.
In next part of multi tier programming you will see a good practice that can be used with full sample code.

Friday, May 9, 2008

Delegate -Part2

Now that we have an understand about the syntax next step is that we need to know some rules about delegate
1- Based on definition we can have different signature of method to assign in delegate
Like:
Public delegate void TestCallback(Person p, int status);
This delegate can accept methods with person and int parameters.
See this example:

Public delegate int Testcallback(int a, int b);
TestCallback tc;
Tc = new TestCallback(Test.f);
int result = tc(5,10);
public class Test
{
Public static int f(int a, intb)
{
Return a*b+b;
}
}

2- The first step of delegation (which is a class definition) will be inside a namespace
3- We always pass address in step 3 of delegate as you can see in last example even delegate seems to accept two parameters we just passed method name since it means it accept methods with two parameters
4- Multiple methods can be assigned to a delegate. It means that a delegate can have more than one method address see this example:
Public delegate void TestCallback();
TestCallback tc;
Tc = new TestCallBack(Test.f);
Tc += new TestCallBack(Test.g);
Tc();
Now when tc is called first f method and then g method will be called
5- Delegate operators are: +, -, +=, -= these operators help you to assign or remove method address from delegate
6- If you closely look into delegate steps what we are doing is that put a method address in delegate then we call delegate to call that method. The question is that why we need that ?Why we don’t call the method directly? Answering to this question is postponed to next lesson of delegate. However, There is a very important point which is we never should use step 3 and step 4 of delegate in one place (like in one method) because we are just wasting our time instead of calling our method we are putting the method in delegate and then we are calling delegate to call the method! We could call the method directly.