Thursday, December 15, 2011

Make a Blueprint to your asp.net application

When i was in 10th, along with the computer, programing language ''C" was also introduced to me. I learn it, wrote codes , executed. But i did not know how useful it would be ! But now in a ship, with Bugs , i know how it belongs to me. OK, I coded all of my code in a single page in a single class for a quite long time, nearly 8 years. But some years back when someone asked me to write code in different pages, classes and when they talked about the architecture , the boy he was in 10th came back, yes he did not know why and how to do that . This is for such boys.


There are various definitions of architecture. One such is, “Architecture is a blue print for application development.” We can develop many similar applications using the same “blue print” once it is implemented successfully in one application.
Often an architecture, which is not suitable for an application may be good for the other. Today, there are numerous kinds of architecture recommended for software development -
  • N-tier architecture
  • SOA architecture.
  • Plug-in architecture
  • DNA architecture and so on.

Architecture is independent of any particular platform or language; it depends specifically as per the system requirements. An architect who designs the architecture would create it based on the client requirements by keeping the following goals in mind.
  1. Readability
  2. Scalability
  3. Maintainability
Once the architecture is ready, designers will design the application using design patterns to accomplish the system requirements. There are various design patterns and tools to design them.

Now, i would like to talk about only one kind of architecture :- an "N-tier architecture" one. Because i don't know about others. Eventually i will add others also. Ok, Lets come back to our topic. N-tier architecture.

OK lets start with a 3-Tier. That is the most common and most used architecture. In three tier architecture, application is separated physically into various tiers such as Presentation tier, Business Tier, and Data Access Tier. Each tier is a separate assembly.

Now look into the following diagram. It has 5 parts. Three different layers , a Database and a part which is common for every layer(We Can call them common objects)






1) Presentation Tier:

Presentation Tier contains graphical display components and files such as ASPX, ASCX, Master pages, Style Sheets, and so on. Usually “Presentation Tier” is a web site or web application. All ASPX or ASCX pages contain HTML design where we add asp.net controls like buttons, textboxes, data controls, validation controls and so on. And all these aspx/ascx pages contain code behind files where we write the page specific code like filling drop downs, binding data controls, validating input data etc.


2)
Business Tier:

Business tier is a class library project with set of classes that are converted into DLL and added as reference in the actual application. It presents data to the higher layers like UI and talks to the Data Access Layer to fetch data.

3) Data Access Tier:

Data Access Tier is a C# class library consisting of set of classes used to encapsulate data access methods like CRUD (CREATE, READ, UPDATE, and DELETE) operations as well as a set of methods to communicate with data store.

4) Common Objects

These are common to all layers. It does the works like passing data from one layer to another,control logging, handle exceptions, security etc.





Please go through each topics to know more about each and how to code it.

renjith


























Sunday, November 20, 2011

Business Tier:

Business tier is a  class library  with set of classes that are converted into DLL and added as reference in the actual application. It presents data to the higher layers like UI and talks to the Data Access Layer to fetch data. Usually it performs the following tasks:

Fetch data from Data Access Layer on which it applies business rules.
Save data after applying rules on it.
Perform validation on data which has to be present or save.

This library should contain a set of classes to communicate with Data Access Layer. It should  act as a mediator between Presentation Layer and Data Access Layer. Presentation layer  communicates with Data Access Layer through this layer and domain objects are used to pass data between layers.Domain objects will be explained latter.

All the business logic required for the application hasto be  defined in this library. For example Vendor related logic in VendorManager class, Product related logic in ProductManager class and so on. All these business classes have to be derived from the interfaces defined IBusiness library.

Now lets see the manager class BluePrintManager: this is the business class inherited from IBluePrintManager

-----------------------------------------------------------------------


namespace IManager
{
public interface I
IBluePrintManager
{
BluePrintInfonfo GetManagerVal();



}

-----------------------------------------------------------------------


namespace  Manager
{


public class  BluePrintManager : IBluePrintManager
{


IBluePrintDAL blueDal = new BluePrintDAL();

 public BluePrintrInfo GetManagerVal()
        {
          
                return blueDal.GetDALVal();
         
        }

}
}


-----------------------------------------------------------------------


You can see something called BluePrintrInfo. This is used to pass data among layers.To know more about this please visit domain objects. BluePrintDAL is Data Access Layer and it is explained in next session.

Thursday, November 10, 2011

Data Access Tier:


Data Access Tier is a  class library consisting of set of classes used to encapsulate data access methods like CRUD (CREATE, READ, UPDATE, and DELETE) operations as well as a set of methods to communicate with data store.
Its primary job is to communicate with any data store like XML, RDBMS, or any Text file etc. It does not contain any business logic. It is used to help communicate with data store to fetch and store data.

This library contains a set of classes. The primary purpose of this library is to communicate with  database.This library does not implement any business logic. 

------------------------------------------------------------------
namespace IDAL

{
 public interface IBluePrintDAL
{
BluePrintInfonfo GetDALVal()
}
}

------------------------------------------------------------------

interface DAL
 {
public class BluePrintDAL : IBluePrintDAL
{
public BluePrintInfonfo GetDALVal()
        {
           Execute sp to get it
           
            return Bluevendor;
        }

}


------------------------------------------------------------------

Saturday, October 1, 2011

Presentation Tier:

Presentation Tier contains graphical display components and files such as ASPX, ASCX, Master pages, Style Sheets, and so on. Usually “Presentation Tier” is a web site or web application. All ASPX or ASCX pages contain HTML design where we add asp.net controls like buttons, textboxes, data controls, validation controls and so on. And all these aspx/ascx pages contain code behind files where we write the page specific code like filling drop downs, binding data controls, validating input data etc.

Now start creating class for this . Here we have two ways. Either we can access the business layer directly by calling the object. another option is clearly separate these  two layers using the design pattern. Lets follow the second method, which is comparatively newer and more professional way of approaching. So Our next requirement is to separate entire business logic from UI.Here we need some design patterns to be implemented . Here i am trying to follow one which is called MVP (Model View Presenter). This is introduced in layer and UI. So, UI cannot access business layer directly, all calls to business layer is routed through a view.



The first step is to create a aspx page. This would be the view part of our web application. Now create a library for presenter. We are going to add a class ie , the presenter for already added view part  . Before doing this i would like to call your attention into one more design pattern. You can avoid this. But using this also would be a nice practice.It is called "Abstract Factory". You cant  use the objects of the classes directly. Instead of that you will have to use something called "Abstract Products" . Its not that complicated. Create the library for the presenter interface first, then add presenterinterface into this.  Now in view , create objects of presenterinterface and initialize using presenterclass. This is called abstract products. Now we achieved two things. First Our business logic is going to be fully separated from your UI. The second , code in business will be added only at run time . It gives amazing flexibility to change your business without changing anything in your view.


Ok, we now just finished our two layers. We created the view where your code behind is located. Then we made interface for presenter to achieve abstract factory design pattern. Now we are calling the abstract product to connect to the business class.Calling business class from the presenter is included in the next topic, ie Business tier. Please check the following code for the first two layers.


 View and presenter


 An ASP.Net web site  contains ASPX, ASCX, Master Pages, Style sheets, Images and Resource files etc. All ASPX and ASCX files are divided into two separate files called as HTML designer and Code behind files. HTML designer has all the ASP.Net web controls. On other hand code behind files are used to write page specific code. All these code behind classes are derived from System.Web.UI.Page and Interfaces defined in Views library where all views (Views in MVP pattern) have been defined.

The first step is to create a view library, create the library and add the following code


---------------------------------------------------------------------------------
namespace Views
{

public interface IBluePrintView 
{
// Define everything required for your  code behind class here

}
}

---------------------------------------------------------------------------------
Now create Code behind class which is  in CS file by inheriting the view.


---------------------------------------------------------------------------------
 
namespace IViews
{

public partial class BluePrint : IBluePrintView

{

//Your internal variables here
BluePrintPresenter localPresenter;

#region OnInit
    /// <summary>
    /// Overriden method OnInit.
    /// </summary>
    /// <param name="e">EventArgs object.</param>
    protected override void OnInit(EventArgs e)
    {
        BlueprintPresenter bluePresenter = new BlueprintPresenter(this);
        bluePresenter.GetPresenterVal();


    }
    #endregion OnInit

}


---------------------------------------------------------------------------------

 Now add presenter library


---------------------------------------------------------------------------------
Namespace presenter

{
public class BluePrintPresenter

{

IBluePrintManager blueManager = new BluePrintManager();

public void GetPresenterVal()
        {

BluePrintInfo blueInfo = blueManager.GetManagerVal();

         }


}
}


---------------------------------------------------------------------------------

Please go to Business Tier to know what is IBluePrintManager and the business .