Thursday, May 15, 2008

Best Practice for Multi tier Applications Architecture

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
There are many ways to implement multi tier applications. You can have different layers. Meanwhile, there are various ways to pass data between layers. First I am going to talk about one of the best practice to arrange your layers.


Look at this picture:

Common Layer
A key layer is Common layer if you look closely to picture you will see that all the layers are using this layer. So it has shared information like your data structure which can be business entities or your typed dataset. (Depend on which approach you choose to pass data. We will see more detail about passing data between layers)
User Interface
User Interface can be Web application or Windows Application. In this layer some classes represents UI. Like Windows forms or Web Forms.

User Provider
Then there is a layer called User Provider which is responsible to provide data the way UI needs. Sometimes objects or data you are retrieving from data base will not match User interface need. Let me give you an example in one of my projects I was asked to show a customize calendar indicating shift information in it. I had to retrieve data from different entities and change those objects to a month and each month had some weeks each week had 7 days each day had shift information data. Then I bounded those data to repeaters recursively (web application). Remember the best practice for this layer suggests that you may have all your methods as static methods.
interface
After UI layer you may want retrieve this data by different technologies like remoting or web services or directly. Let me give you an example again in one of my projects I was asked to implement a web application but the functionality should be implemented in a way that data could be loaded from a web service as well. So I had an interface then I configured this interface to load data from a dll which was my Facade layer. Then I told them all the functionalities are implemented in my facade layer all they need is that implementing a web service calling my facade layer and configuring the interface to connect to that web service. So when you configure interface then you can specify to which layer you need to connect. So sometimes you can make it web service or a dll or a remoting service. Spring is a library that you can use then you can take advantage of loosely coupled feature. I will get back to spring later on. Or you can use abstract factory pattern. So this layer which is an interface is very critical and makes your application so maintainable.
Facade Layer
Facade Layer is a place that you can connect from UI so main purpose of this layer is that somehow separate your User Interface form backend and business layers. Also in this layer we may check security and do some logging. Every class here better just connect to business layer however, some people believe that connecting to data access through this layer is fine. Nevertheless, I should say if you accept just one function call then you better just connect to business layer so you have more flexibility later on.
Business layer
Business layer helps you to apply all your business rules. Also since all request passing from this layer , you can apply business to your classes in later steps.
Data Access
Data Access Layer is the place where you save and retrieve data from data base.

Passing data through layers

There are different ways:
1- Using Xml Structure
2- Using untyped data set
3- Using Typed data set
4- Using Business entities
5- Using simple parameters
I should say a lot of these ways has its own advantages and disadvantages. The best way to do that is using Business entities which give you more flexibility and you can apply design patterns approach on them. Like proxy patterns. Also it is now fully supported by .net.

In next lesson I am going to discuss a code also I am going to talk about some helper tools to facilitate your layers like: Code smith, NLog and Spring. Also I will discuss object data binding in .net especially in web applications. See Next Lesson for sample code

5 comments:

Anonymous said...

از پست هاي بسيار عالي شما ممنون. به خصوص اين مطلب واقعا مختصر و مفيد بود.
اميدوارم اين بلاگ با همين سرعت و با همين كيفيت به كار خودش ادامه بده و البته ما از شما عقب نيفتيم.
شاگردِ شاگرد شما!

Shahram said...

Hi Emad,
I'm so happy to see these valuable posts which come from years of experiences. Great Job!

Nahid said...

Dear Emad,
The way that you state problems and their solutions is really fabulous.
I appreciate your manner of sharing your valuable knowledge with others.

Best Wishes

S said...

This was a great article. After searching for hours and hours trying to find a decent article on multi tiered architecture that has some helper code and when I was just about to give up, came across this article. I wish my own lecturer had explained it so well, I was really struggling with trying to get my concepts clear. I was so glad to come across this one. Thanks.

Anonymous said...

I appreciate your knowledge sharing. Thankes