I was looking for advice, feedback and information on best practices when designing ASP.NET Web Forms in a .NET web application.
Allow me to post a sample of how I currently do things. I am omitting the ASPX page as it only has 2 textboxes and a button. PersonEntity validates, loads, and saves in this example. Real-world would have a "Person" business object that saves, loads.
The Codebehind file of an "Add / Edit" page:
public partial class PersonAddUpdate : System.Web.UI.Page
{
public int PersonId
{
get
{
int _personId = 0;
if (Int32.TryParse(Request.QueryString["id"], out _personId))
{
return _personId;
}
else
{
return 0;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (PersonId != 0)
{
var person = GetPersonById(PersonId);
if (person != null)
{
BindFormToData(person);
ViewState.Add("PersonEntity", person);
}
}
}
}
protected bool BindDataToForm(PersonEntity person)
{
person.FirstName = txtFirstName.Text;
person.Lastname = txtLastName.Text;
ViewState.Add("PersonEntity", person);
// internal entity validation
return person.Validate();
}
protected void BindFormToData(PersonEntity person)
{
txtFirstName.Text = person.FirstName;
txtLastName.Text = person.Lastname;
}
protected PersonEntity GetPersonById(int personId)
{
PersonEntity person = new PersonEntity();
return person.LoadById(personId);
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}
PersonEntity person;
if (ViewState["PersonEntity"] == null)
{
person = new PersonEntity();
}
else
{
person = (PersonEntity)ViewState["PersonEntity"];
}
if (!BindDataToForm(person))
{
// failed entity validation
return;
}
if (!person.Save())
{
// problem saving
return;
}
else
{
Response.Redirect("PersonDetail.aspx?id=" + person.Id.ToString());
}
}
}
Workflow:
Page Load: Get the id from querystring. If a problem arises, return 0. No records in the system have a "0" id. Try to load data. If there's data, set the form values and add it to viewstate.
On Save: Stop if page is invalid. Create an uninitialized PersonEntity. If viewstate contains an entity, then form binding (gets form values) routine does not need new PersonEntity. If viewstate is null, then we are a new record.
Call BindDataToForm which gets form field values and returns false if it fails internal entity validation. Attempt Save and continue if OK.
Questions:
I've been using this pattern for a while now. Do you see anything I can do to improve it? I was trying to find samples of complete forms that handle Add / Edit, but I was unable to find ones that worked as good as this with ORM-generated code.
Any comments, advice, ideas, or questions?
I hope the code is simple to read and understand.