I am working on a Winforms application with EF database-first approach, when my application loads in the home page I need to get some data and populate the form.
Most of the controls on my form are combobox controls which get data from database tables and there are approximately 20 combobox controls, so this means I have almost 20 methods which are calling the database and bind the data to combobox controls.
I invoke these methods in the Form_Load
method.
I am not satisfied with the approach I followed, need suggestions on how this can be improved.
My BAL looks like below.
Public List<ContractEngineers> GetContractEngineers()
{
using(EmployeeEntities contractEng = new EmployeeEntities())
{
List<ContractEngineers> contractEngineersList = new List<ContractEngineers>();
contractEngineersList = /* Code that gets the total list of contract employees. */
}
}
Public List<PayRollEmployees> GetPayRollEmployees()
{
using(EmployeeEntities contractEng = new EmployeeEntities())
{
List<PayRollEmployees> payRollEmployeesList = new List<PayRollEmployees>();
payRollEmployeesList = /* Code that gets the total list of payroll employees. */
}
}
There are almost 20 methods on my form which look similar, they get data and bind to a combobox, in order to make this better, I created a common class called 'CommonComboBox' which can be used for all the comboboxes and I have removed all the classes like 'ContractEngineers', 'PayRollEmployees'
public class CommonComboBox
{
public virtual int Id {get;set;}
public virtual string Name {get;set;}
}
My question is: instead of using the using block in all the methods which just open the connection and close the connection, is it a better approach declaring initializing
EmployeeEntities EMPEntities = new EmployeeEntities()
at the top and using the object EMPEntities
in all the methods?
Need some suggestions on how this code can be improved as I am not happy with this code