Thursday, March 29, 2012

Separating presentation and data access tier. Best Practices for writing simple/intermedia

Hi,

I am writing a simple code, where I am a accessing database table, and based on the content of the table I dynamically generate a webform. In the webform I have a few buttons which when clicked do some dataaccess.

I have a class that performs all the dataacess, another class that I use to place the controls on the webform.
I am not sure if the code design is appropriate. The presentation and biz logic layer are not completely separated. I would like to know some best practice coding tips from the experianced ppl here. Or if anyone can send a pseudocode of his/her coding methods.

I would like to know when writing simple modules, which typically involves some dataaccess, data manipulation and then data presentation, which layer should go into the events like Page_load and CmdButton_click.

In the code below, I am not quite comfortable with SQL statement generation in the Page_Load event

My code structure looks like


Page_load()
{

string SQL="some sql stmt"
DataSet dsdata=new DataSet()
dsdata=dataaccessclass.getdata(SQL)

foreach(datarow dr in dsdata)
{

if(some condition)
//create control A
//set control properties based on dr
//call addcontrolclass.placecontrolAfunction
else
{

}

}

Cmd_Button_click()
{

//check which button is clicked
//make a SQL statment as string
//call dataaccessclass.performoperation(SQL)

}

Hey there,

I find when I'm writing, the best way to keep it all separate is to write a bindcontrols() procedure that makes calls to the logic classes/methods, this way populating your controls is done by calls the furthest away you can get from your presentation layer.

Hope it helps, it is saturday night where I am and I have had a few brewskies!

Cheers

Steve
I would seriously contemplating looking at the MS Data Access Applcation Blocks. I Class Library that takes care of your DAL quite elegantly (it's free, and all the source code is included, and it's really simple to use):

Data Access Applcation Blocks

0 comments:

Post a Comment