Friday, May 16, 2008

Sample of Delegate in a web application



You hardly can find a sample of delegate in books. Also in my classes people ask me “Could you give us some real example of delegate?” Ok this is a real sample of delegate that I used for one of my applications:


First let’s understand the application:

The application was a web application. It had a master page with 6 tabs and 3 buttons (Save all, submit data, Exit). Each tab had to show different information so I had 6 pages using this master page. Client asked me to save information when another tab is selected or when they click on save all or submit data (when submit data was selected just application was redirect to another part).Any way my situation was like this some buttons in master page and also in pages I had a save method to save all information about that page. I needed to call save method of the pages when in master page one of those buttons was clicked. Like this:

So I created a saved delegate. In master page I had an instance of this delegate then in button clicks I called that delegate. In each page load I registered the save method into delegate so when in master page saved delegate is called then this save method of page is called actually. Master page does not know what is the method registered in delegate. It is just calling delegate. In fact pages register their own save method in delegate so when delegate is called save method will be called.


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