The database schema is as follows
Database Table : Employee
EmployeeID(int, pk) FirstName(nvarchar) LastName(nvarchar) Email(nvarchar) EmployeeType(int) Salary(decimal) HourlyRate(decimal)
Database Table : Benefits
BenefitID (int, pk) Benefit (nvarchar) EmployeeID (int, fk)
Employee table holds all the employee data. Salary field applies to fulltime employee and the hourly rate applies to hourly employee. Benefits only apply to Full Time employee.
I have setup a service class that will return all the employees. I also want to initialize all the benefits to the Salaried Employee object. I have setup a check for employeetype using a switch statement and then create the instance of the object and add it to the collection.
This will work perfectly. The issue I have is every time I add a new type of employee then I will have to modify the Employee service and add logic to switch statement to handle the new type of employee. This breaks the open close principle.
My question is what is the best way to load data from the database to the objects in this type of scenario? Please help me understand.
public enum EmployeeType
{
FullTime =1,
PartTime =2
}
public class EmployeeBenefit
{
private string _benefit;
private Employee _employee;
public int EmployeeId { get { return Employee.Id; } }
public Employee Employee { get { return _employee; } }
public string Benefit { get { return _benefit; } }
public EmployeeBenefit(Employee emp, string benefit)
{
_benefit = benefit;
_employee = emp;
}
}
public abstract class Employee
{
public int Id { get; set; }
public abstract EmployeeType Type { get; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
public class SalariedEmployee: Employee
{
private ICollection<EmployeeBenefit> _benefits;
public SalariedEmployee()
{
_benefits = new List<EmployeeBenefit>();
}
public IEnumerable<EmployeeBenefit> Benefits { get {return _benefits;} }
public double Salary { get; set; }
public override EmployeeType Type
{
get { return EmployeeType.FullTime; }
}
public void AddBenefit(string benefit)
{
EmployeeBenefit ebenefit = new EmployeeBenefit(this, benefit);
if (Benefits.Contains(ebenefit))
_benefits.Add(ebenefit);
}
public void RemoveBenefit(string benefit)
{
EmployeeBenefit ebenefit = new EmployeeBenefit(this, benefit);
if(_benefits.Contains(ebenefit))
_benefits.Remove(ebenefit);
}
}
public class PartTimeEmployee : Employee
{
public override EmployeeType Type
{
get { return EmployeeType.PartTime; }
}
public double HourlyRate { get; set; }
}
public class EmployeeService
{
public IEnumerable<Employee> GetAll() {
List<Employee> _lst = new List<Employee>();
using(var db = new EmployeeContext)
{
foreach(Employee e in db.Employees)
{
switch(e.Type){
case EmployeeType.PartTime:
PartTimeEmployee pt = new PartTimeEmployee();
//initialize the values of parttime employee like hourly rate
_lst.Add(pt);
break;
case EmployeeType.FullTime:
SalariedEmployee se = new SalariedEmployee();
//initialize the value of salaried employees like benefits, salary
_lst.Add(se);
break;
}
}
}
return _lst;
}
}
Aucun commentaire:
Enregistrer un commentaire