我正在编写一个使用列表来存储Employee数据的程序,遵循SOLID原则,我试图将尽可能多的功能从主类移到自己的类中。我有一个存储库类,它列出了初始列表值,并包含读取和创建方法来访问和编辑列表。我的问题是,我的新类不能访问随后添加到列表中的任何内容,只能看到引导时出现的尾数据。
这是我的Repo类:
public class Repository
{
private List<EmployeeData> myEmployeeData = new List<EmployeeData>()
{
new EmployeeData()
{
EmployeeID = 1,
FName = "Joe",
LName = "Bloggs",
isPermanent = true,
Salaryint = 40000,
Bonusint = 5000,
DayRateint = null,
WeeksWorkedint = null
},
new EmployeeData()
{
EmployeeID = 2,
FName = "John",
LName = "Smith",
isPermanent = true,
Salaryint = 45000,
Bonusint = 2500,
DayRateint = null,
WeeksWorkedint = null
},
new EmployeeData()
{
EmployeeID = 3,
FName = "Clare",
LName = "Jones",
isPermanent = false,
Salaryint = null,
Bonusint = null,
DayRateint = 350,
WeeksWorkedint = 40
}
};
public EmployeeData Create(int IDcount, string fname, string lname, bool isPerm, int? Salary, int? Bonus, int? DayRate, int? WeeksWorked)
{
var createEmployee = new EmployeeData()
{
EmployeeID = IDcount,
FName = fname,
LName = lname,
isPermanent = isPerm,
Salaryint = Salary,
Bonusint = Bonus,
DayRateint = DayRate,
WeeksWorkedint = WeeksWorked
};
myEmployeeData.Add(createEmployee);
return createEmployee;
}
public IEnumerable<EmployeeData> ReadAll()
{
return (myEmployeeData);
}
public EmployeeData Read(int employeeID)
{
return myEmployeeData[employeeID];
}
public EmployeeData Update(int employeeID, string fname, string lname, bool isPerm, int? Salary, int? Bonus, int? DayRate, int? WeeksWorked)
{
var x = Read(employeeID);
x.FName = fname;
x.LName = lname;
x.isPermanent = isPerm;
x.Salaryint = Salary;
x.Bonusint = Bonus;
x.DayRateint = DayRate;
x.WeeksWorkedint = WeeksWorked;
return x;
}
public bool Delete(int employeeID)
{
myEmployeeData.RemoveAt(employeeID);
return true;
}
}
这是我的新类不能访问新创建的项目:
public class Calculator : Repository
{
public double AnnualPayAfterTax;
public double AnnualPay;
public double CalculateEmployeePay(int employeeID)
{
bool EmploymentStatus = Read(employeeID).isPermanent;
if (EmploymentStatus == true)
{
int Salary = (int)Read(employeeID).Salaryint;
int Bonus = (int)Read(employeeID).Bonusint;
AnnualPay = Salary + Bonus;
}
else {
int DayRate = (int)Read(employeeID).DayRateint;
int WeeksWorked = (int)Read(employeeID).WeeksWorkedint;
AnnualPay = (DayRate * 5) + WeeksWorked;
}
if (AnnualPay < 12570) { AnnualPayAfterTax = AnnualPay; }
if (AnnualPay > 12570) { AnnualPayAfterTax = (AnnualPay - 12570) * 0.2; }
return (AnnualPayAfterTax);
}
}
这是我的main方法的一部分,它调用了两个类,注意read()在这个类中对于新添加的列表项工作得很好。
bool CalLoop = false;
while (CalLoop == false)
{
Console.Clear();
Console.WriteLine("CALCULATE ANNUAL PAYn");
Console.WriteLine(string.Concat(re.ReadAll()));
Console.Write("nSelect ID of Employee: ");
string Input = Console.ReadLine();
bool valid = int.TryParse(Input, out Output);
if (valid)
{
int selectedID = Output;
selectedID = selectedID - 1;
bool CheckID = (selectedID <= IDcount);
if (CheckID)
{
Console.WriteLine("Employee Name: " + re.Read(selectedID).FName + " " + re.Read(selectedID).LName);
Console.WriteLine("Employment Type: " + re.Read(selectedID).isPermanent);
Console.WriteLine("Annual Pay after Tax: £" + cal.CalculateEmployeePay(selectedID));
}
else
{
Console.WriteLine("Invaild ID.");
}
}
else
{
Console.WriteLine("Invaild ID.");
}
CalLoop = true;
}
知道为什么我的新类不能看到任何新添加到列表中的东西吗?我认为列表在整个运行时动态更新?我错过什么了吗?
感谢任何帮助或评论,所以提前感谢你!
有两个类:
public class Repository
{
public List<EmployeeData> myEmployeeData = new List<EmployeeData>();
}
public class Calculator : Repository
{ }
当这两个类实例化时,您可以看到它们不共享相同的列表:
var rep = new Repository();
var cal = new Calculator();
if(object.ReferenceEquals(rep.myEmployeeData, cal.myEmployeeData))
{
Console.WriteLine("Repository and Calucaltor share the same liste.")
}
else
{
Console.WriteLine("Repository and Calucaltor don't share the same liste.")
}
一个类就像一个计划,以建立一个建筑物。例如,计划"BaseBuilding"有接待处和洗手间。还有一个"办公楼"计划。扩展(继承)从计划"BaseBuilding"并添加一些办公室工作
接下来,您按照计划构建一个建筑物"BaseBuilding"和其他建筑的规划"办公楼"。在本例中,您构建了两个接待处和卫生间(一个接一个)。
在实例化Repository
和Calculator
时也是一样的。这将创建两个不同的对象,其中每个对象都有自己的列表。有关更多信息,请参阅有关继承的官方文档;
但这才是真正的问题。当类a从类B继承时,可以用a替换B。在您的情况下,Calculator
继承自Repository
,那么您可以用Calculator
替换Repository
。
I use the repository to read employee's informations.
I use the calculator to read employee's informations.
第一句很清楚,但第二句…听起来很奇怪。这是因为这违反了第三个SOLID原则(Liskov替换)。
为什么计算器可以读取员工的信息?检索员工信息是存储库的职责。计算器只能做计算。这就违背了第一个SOLID原则(单一职责)。
如果你想遵循SOLID原则,那么我建议你避免继承,而选择组合。
在你的例子中:
class Calculator
{
private readonly Repository _repository;
public Calculator(Repository repository)
{
_repository = repository;
}
public double CalculateEmployeePay(int employeeID)
{
bool EmploymentStatus = _repository.Read(employeeID).isPermanent;
...
}
}
所以计算器只计算和存储库只管理员工。
private List<EmployeeData> myEmployeeData = new List<EmployeeData>()
isprivate
…修改为protected
,以便继承的类可以访问它。