How do you perform CRUD from code behind (web form)?
One of my students did it this way (Direct Approach):
Note: Some codes are omitted to simplify the example and focus on the real topic.
//read
DDL.DataSource = DbConsolidator.GetNameList();
DDL.DataTextField = "Name ";
DDL.DataValueField = "ProfileId";
DDL.DataBind();
lblAccount = reader["Account"].ToString();
//create & update
cmd.Parameters.AddWithValue("@ProfileId", DDL.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Account", txtAccount.Text);
//delete
cmd.Parameters.AddWithValue("@AccountId", DD2L.SelectedItem.Value);
My other student did it this way (Layered Approach):
//read
GV.DataSource = DbConsolidator.GetAccount(Int32.Parse(DDL.SelectedItem.Value);
//create & update
DbConsolidator.AddAccount(txtAccount.Text, Int32.Parse(DDL2.SelectedItem.Value), etc........);
Which approach do you consider a best practice for working on CRUD from Code Behind?
Okay, I know the codes are not clear (and won't even compile, LOL), but the purpose of this post is to know which approach is better.
80% of my projects, I worked with N-Layer architecture. So I prefer Layered Approach over Direct Approach. But there are times that Direct Approach is a better option only if it's really a "small project".
No comments:
Post a Comment