我目前正在学习 DI 和 IoC 原则。所以,我偶然发现了这样一个场景,我有一个无法通过构造函数注入的依赖项。另外,我不能将它作为方法参数传递,因为该实例的创建是有条件的+它只能用它自己的参数实例化。这是我的 Employee 和 WorkYear 类的超级简化版本:
public abstract class Employee
{
private List<WorkYear> _workYears;
// other private fields....
protected Employee(IDependency1 dep, etc...)
{
WorkYears = new List<WorkYear>();
// other components initialization....
}
public IEnumerable<WorkYear> WorkYears
{
get => _workYears.AsReadOnly();
private set => _workYears = value.ToList();
}
public void StartWorking(DateTime joinedCompany)
{
List<PayPeriod> periods = // Calculating periods...
WorkYear year = WorkYears.FirstOrDefault(y => y.CurrentYear == joinedCompany.Year);
if (year == null)
{
// Here is the problem:
year = new WorkYear(joinedCompany.Year, this, periods);
AddYear(year);
}
else
{
// Logic when year not null
}
year.RecalculateAllTime();
}
public void AddYear(WorkYear workYear) => _workYears.Add(workYear);
// More code...
}
public class WorkYear
{
public WorkYear(int currentYear, Employee employee, List<PayPeriod> periods)
{
Employee = employee;
EmployeeId = employee.Id;
CurrentYear = currentYear;
PayPeriods = periods ?? new List<PayPeriod>();
foreach (PayPeriod period in PayPeriods)
{
period.WorkYear = this;
period.WorkYearId = Id;
}
}
// More code...
}
如您所见,如果 Employee 还没有 WorkYear 的新实例,我只需要它。我发现一个线程建议使用简单的工厂类来解决类似的问题。这样的解决方案可以工作,但是如何处理无法实例化 WorkYear 的参数?
很高兴看到如何解决这个问题的例子。
一只甜甜圈
相关分类