I have a finance web-application in ASP.NET WebForms using Telerik RAD controls. Two event handlers wound up being very similar in implementation.
What steps could I take to improve the organization of and/or the efficiency or DRY-ness of the code? What other issues / code-smells are present?
The Code
Update Event
protected void gv_FinancePositionList_UpdateCommand(object sender, GridCommandEventArgs e) { var item = (GridEditFormItem) e.Item; int financeListId = int.Parse(item.GetDataKeyValue(keyName: "FinanceListId").ToString()); int companyId = int.Parse(((RadComboBox) item.FindControl(id: "ddl_Company")).SelectedValue); int year = ((RadMonthYearPicker) item.FindControl(id: "pick_Year")).SelectedDate.Value.Year; int periodTypeId = int.Parse(((RadioButtonList) item.FindControl(id: "rbtn_PeriodTypeName")).SelectedValue); int partition = 1; if (((RadDropDownList) item.FindControl(id: "ddl_PeriodType")).Visible) { partition = int.Parse(((RadDropDownList) item.FindControl(id: "ddl_PeriodType")).SelectedValue); } string cashAndEquivalents = ((RadNumericTextBox) item.FindControl(id: "txt_CashAndEquivalents")).Text; decimal? cashAndEquivalentsVal = null; if (!string.IsNullOrEmpty(cashAndEquivalents)) { cashAndEquivalentsVal = decimal.Parse(cashAndEquivalents); } string shortTermInvestments = ((RadNumericTextBox) item.FindControl(id: "txt_ShortTermInvestments")).Text; decimal? shortTermInvestmentsVal = null; if (!string.IsNullOrEmpty(shortTermInvestments)) { shortTermInvestmentsVal = decimal.Parse(shortTermInvestments); } using (var ctx = new Model()) { FinancePositionList financePositionList = ctx.FinancePositionLists.Find(financeListId); financePositionList.CompanyId = companyId; financePositionList.CashAndEquivalents = cashAndEquivalentsVal; financePositionList.ShortTermInvestments = shortTermInvestmentsVal; ctx.SaveChanges(); } }
Insert event
protected void gv_FinancePositionList_InsertCommand(object sender, GridCommandEventArgs e) { var item = (GridEditFormItem) e.Item; int companyId = int.Parse(((RadComboBox) item.FindControl(id: "ddl_Company")).SelectedValue); int year = ((RadMonthYearPicker) item.FindControl(id: "pick_Year")).SelectedDate.Value.Year; int periodTypeId = int.Parse(((RadioButtonList) item.FindControl(id: "rbtn_PeriodTypeName")).SelectedValue); int partition = 1; if (((RadDropDownList) item.FindControl(id: "ddl_PeriodType")).Visible) { partition = int.Parse(((RadDropDownList) item.FindControl(id: "ddl_PeriodType")).SelectedValue); } string cashAndEquivalents = ((RadNumericTextBox) item.FindControl(id: "txt_CashAndEquivalents")).Text; decimal? cashAndEquivalentsVal = null; if (!string.IsNullOrEmpty(cashAndEquivalents)) { cashAndEquivalentsVal = decimal.Parse(cashAndEquivalents); } string shortTermInvestments = ((RadNumericTextBox) item.FindControl(id: "txt_ShortTermInvestments")).Text; decimal? shortTermInvestmentsVal = null; if (!string.IsNullOrEmpty(shortTermInvestments)) { shortTermInvestmentsVal = decimal.Parse(shortTermInvestments); var financePositionList = new FinancePositionList { CompanyId = companyId, CashAndEquivalents = cashAndEquivalentsVal, ShortTermInvestments = shortTermInvestmentsVal } using (var ctx = new Model()) { ctx.FinancePositionLists.Add(financePositionList); ctx.SaveChanges(); } }
companyId
,year
, etc. in one place. This IS binding of a kind. @eurotrash is referring to implementingIPropertyChanged
which binds on a per-control event-driven (automatic) basis. Either way an object is populated with the UI control values; and then all the subsequent work is done using this business object. \$\endgroup\$