저는 복잡한 비즈니스를 수행하는 프로젝트를 진행하고 있습니다. AccountService와 SchoolService의 두 클래스를 고려하십시오.Anemic 도메인 모델을 사용하는 서비스 간의 순환 참조
저는 Unity와 웹 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)
{
...
}
}
이 프로젝트의 BLCs의 70 % 상황이다 : 여기
는 일례이다.
프로젝트에서 순환 의존성의 예를들 수 있습니까? – user1849310
가난한 디자인 같은 소리 ... 나는 일반적인 서비스를 제 3의 서비스로 분해 할 것입니다. 그것은 순환 종속성을 해결할 것입니다. DI 엔진은 일반적으로 순환 참조에서 예외를 throw합니다. – SledgeHammer
@SledgeHammer 그 이상입니다. DI 없이도 어떻게 문제를 해결할 수 있습니까? DI는 마술 적이 지 않습니다. 만약 당신이 그것을하지 않으면 그것을 할 수 없다면, 그것을 할 수 없습니다. – Aron