Monday, June 29, 2009

N Tier Architecture

Some Myths

People normally think if they have created UI (ASP.Net application project), BL (.Net Class Library Project), & DAL data access layer (.Net Class Library Project) and referencing these projects in their web project, they are following 3 tier architecture. I think creating a n tier application is more than this.

What is tier?

According to me tier means a stand-alone process which has some code to process. In the above scenario where ASP.Net Web Application is referencing BL and DAL, all the code is running in w3wp (server OS)/ aspnet_wp process which is known as worker process. So in real terms all this is single tier.

Then question arises that, is it 1 tier application? If you are not dealing with databases or any other thing which has its own process then yes it is 1-tier architecture. Database makes it 2-tier architecture. As all the databases are run under some process. sqlservr is the process for SQL databases.

Some people also say that a web application will always run under a browser and why don't treat that as a tier and say this as 3 tier architecture?

I would say a tier should contain some meat in it. Meat means, some code which processes some part of the request handling, it should minimize the effort of request processing. Now what all can be processed at browser? To process anything on browser one has to write java script or any other client script.

Browser can do validation, however every one should be agree that all the validation which we do on client should be done on server also. So browser do not minimize any processing here.

Browser can also enhance the UI which is instant action. That's a good help in enhancing the UI, now logically it is a part of UI layer which is been run on browser instead of server just to improve the user experience it is not helping in request processing.

What makes 3 tier architecture

One has to introduce one more process somewhere. We can host the Business Logic as a WCF service / Remoting. WCF service should be hosted in an application pool other than the web application pool. This is just to make sure that both the application will be run in different worker process.

In this way we would have 3 different processes 1. Web Application Process, 2. BL WCF process, and 3 would be the database process.

Why to make 3-tier

Going back to the starting why did you want your application to be 3-tier. Answer to this question contains many big words like Performance Optimization, Scalability, Robustness, Code Re-Usability, and many more.

Compare this with following question:

2 person to do your job vs 3 person to do your job, you will get all your answers. 3 men can do the task more rapidly, correct? Yes if task can be broken into sub tasks. Same applies here if you can divide your task in sub tasks then 3 tier architecture will be beneficial.

Some people take the following approach in writing the code:

Lets take an example when one is creating a inventory application, now they write a method in DAL PlaceOrder (which takes many arguments like Customer, collection of products, PersonBillingPlan etc), this method will call a SP sp_PlaceOrder and all the logic will be written in that SP. This DAL method will simply be called in BL and will return success / failure. Now even if we create WCF service and force it to run in different process, we won't achieve what we intended to while following 3-tier architecture. This is because we do not have any meat (code to execute in BL), hence all the load is on the database. Database has to perform all the operation of validating data and applying business rules. Now in a team of 2 people if one is over burdened then will my team give the right productivity as expected? I guess no... what do you say please let me know in the comments section.

One more interesting question arises here while following true 3-tier architecture

People say, if we have to write business logic in .Net code then we have to fetch some extra data from Sql to .Net and write code on them, where in SP approach everything was coded in SQL and no data was travelling from SQL to .Net and finally a flag was sent from the DB which signifies success or failure. Now to counter attack this question I would say following things:

1. Data is anyway fetched from database, select query will be hit in both the cases only thing is data will be travelled from sql to .Net. I guess this would not harm that much but depends.

2. Data caching: data caching is way to minimize the above damage. One can easily write code to use data caching at DAL and save the effort of fetching common/static data. If data will be cached then it would really enhance the performance of the application by leaps and bounds.

I hope this would help you writing 3-tier application.