Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring a list of objects to implement a business rule
    primarykey
    data
    text
    <p>I need to refactor the following class:</p> <pre><code>public interface IEmployee { int VacationWeeks { get; } int YearsWithCompany { set; get; } double Salary { set; get; } } public class Employee : IEmployee { private readonly int vacationWeeks; public Employee(int vacationWeeks) { this.vacationWeeks = vacationWeeks; } public int VacationWeeks { get { return vacationWeeks; } } public int YearsWithCompany { set; get; } public double Salary { set; get; } } </code></pre> <p>I need to make sure that VacationWeeks depends only on YearsWithCompany, and I am loading the mapping from the database. So far I have come up with this:</p> <pre><code>public class EmployeeNew : IEmployee { private Dictionary&lt;int,int&gt; vacationWeeksTable; public EmployeeNew(Dictionary&lt;int, int&gt; vacationWeeksTable) { this.vacationWeeksTable = vacationWeeksTable; } public int VacationWeeks { get { return vacationWeeksTable[YearsWithCompany]; } } public int YearsWithCompany { set; get; } public double Salary { set; get; } } </code></pre> <p>This class implements what I want, but it still has one vulnerability: different instances of EmployeeNew in the same collection may have been created with different instances of vacationWeeksTable. </p> <p>All instances of EmployeeNew in the same collection must refer to the same vacationWeeksTable.</p> <p>The application I am refactoring uses lots of List all over the system, and we need to be able to modify YearsWithCompany and Salary, yet to guarantee that only one vacationWeeksTable is used per List. These lists are iterated several times; its elements are modified in each iteration.</p> <p>Here is my imperfect solution. Suggestions are welcome:</p> <pre><code>// this class does two things, which I do not like public class EmployeeList : IEnumerable&lt;IEmployee&gt;, IEmployee { private Dictionary&lt;int, int&gt; vacationWeeksTable; private List&lt;EmployeeSpecificData&gt; employees; private int currentIndex; private EmployeeSpecificData CurrentEmployee { get { return employees[currentIndex]; } } public IEnumerator&lt;IEmployee&gt; GetEnumerator() { for (currentIndex = 0; currentIndex &lt; employees.Count; currentIndex++) { yield return this; } } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public int VacationWeeks { get { return vacationWeeksTable[YearsWithCompany]; } } // this is ugly repetitive code I don't like public int YearsWithCompany { get { return CurrentEmployee.YearsWithCompany; } set { CurrentEmployee.YearsWithCompany = value; } } // this is ugly repetitive code I don't like public double Salary { get { return CurrentEmployee.Salary; } set { CurrentEmployee.Salary = value; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload