我正在从事一个具有复杂业务的项目。考虑两个类:AccountService 和 SchoolService
我正在使用 Unity 和 Web API 的依赖解析器在构造函数中实现依赖注入。
学校服务在某些方面使用账户服务,也有账户服务使用学校服务。所有这些都是项目业务所必需的。这将导致循环依赖,并且不可能将方法从一个类移动到另一个类。
您能否提供有关如何解决此问题的任何想法?
这是一个例子:
public class SchoolBLC : ISchoolBLC
{
public School GetSchool(int schoolId)
{
...
}
public bool RenewRegistration(int accountId)
{
bool result = true;
IAccountBLC accountBLC = new AccountBLC();
// check some properties related to the account to decide if the account can be renewed
// ex : the account should not be 5 years old
// check the account created date and do renewal
return result;
}
}
public class AccountBLC : IAccountBLC
{
public void ResetAccount(int accountId)
{
ISchoolBLC schoolBLC = new SchoolBLC();
School accountSchool = schoolBLC
// get the school related to the account to send a notification
// and tell the school that the user has reset his account
// reset account and call the school notification service
}
public Account GetAccount(int accountId)
{
...
}
}
两个类相互引用,这是项目中 70% 的 BLC 的情况。