protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
loadCountries();
loadRegions();
loadCities();
}
}
private void loadCountries()
{
Country country = new Country();
ddlCountry.DataSource = country.GetDataTable();
ddlCountry.DataTextField = "countryName";
ddlCountry.DataValueField = "countryID";
ddlCountry.DataBind();
}
private void loadRegions()
{
Region region = new Region();
ddlRegion.DataSource = region.GetRegionID(ddlCountry.SelectedValue);
ddlRegion.DataTextField = "regionName";
ddlRegion.DataValueField = "regionID";
ddlRegion.DataBind();
}
private void loadCities()
{
City city = new City();
ddlCity.DataSource = city.GetCityID(ddlRegion.SelectedValue);
ddlCity.DataTextField = "cityName";
ddlCity.DataValueField = "cityID";
ddlCity.DataBind();
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
loadRegions();
if (ddlRegion.SelectedItem.Text == "No Province")
{
ddlRegion.Enabled = false;
loadCities();
}
else
{
ddlRegion.Enabled = true;
loadCities();
}
}
The code is back-end of Default.aspx (Presentation layer)
Any business logic related to Country is put into Country class, same rules applied to Region and City.
Is this snippet design OK? In other words, does it meet the presentation layer standard design? How can i improve this snippet design (if possible)?
I’m new to this, i try to make sure i take it slowly but surely.
I think the naming conventions need some work. Think carefully about the names of your classes and methods.
For example (class name):
You have a class called Country which suggests it represents a country. But, I don’t think it does. It looks like it’s responsible for creating a datatable of countries.
and another example (method name):
You have a method called Region.GetRegionID(). It looks (but I’m not completely sure) as if this gets a Region based on a RegionId so i’d prefer GetByRegionId. The same critism of the name of the class applies.