依赖注入中的循环引用

Circular reference in dependency injection

我正在 Net 5 中创建一个 RESTful api,根据说明,我必须创建使用它们的存储库和服务。逻辑必须在服务中。

我拥有的服务是:

子组服务 群组服务

我遇到的问题是我生成了一个循环引用,因为在 GroupsService 中我需要一个 SubGroupsService 方法,而 SubGroupsService 我需要一个 GroupsService 方法。

将GroupsService服务注入到SubGroupsService没有问题,但是将SubGroupsService注入到GroupsService会产生循环引用。

请你告诉我如何解决这类问题,因为我对依赖注入没有太多经验。

子组服务

public class SubGroupService: ISubGroupService
{       
    private readonly ISubGroupRepository _SubGroupRepository;
    private readonly IGroupService _GroupService;        
    public SubGroupService(
        ISubGroupRepository SubGroupRepository,
        IGroupService GroupService
    {          
        _SubGroupRepository = SubGroupRepository;
        _GroupService = GroupService;        
    }

    public async Task InsertSubGroupService(Subgroup subgroup)
    {
        var group = await _GroupService.GetGroupService(subgroup.idgroup);
        
        if (group != null)
        {
            await _SubGroupRepository.InsertSubGroupRepository(subgroup);
        }
        else
        {
            throw new BusinessException("This group not exists");
        }
    }

    public async Task<Subgroups> GetSubGroupService(int idgroup)
    {
        return await _SubGroupRepository.GetSubGroupRepository(idgroup);
    }
}

集团服务

public class GroupService : IGroupService
{
    private readonly ISubGroupService _SubGroupService;
    private readonly IGroupRepository _GroupRepository;
    public GroupService(
        ISubGroupService SubGroupService,
        IGroupRepository GroupRepository)
    {
        _SubGroupService = SubGroupService;
        _GroupRepository = GroupRepository;
    }

    public async Task<bool> DeleteGroupService(int Idgroup)
    {
        var existsSubGroup = await _SubGroupRepository(Idgroup);
        if(existsSubGroup == null)
        {
            return await _GroupRepository.DeleteGroupRepository(Idgroup);

        }
    }

    public async Task<Groups> GetGroupService(int Idgroup)
    {
        return await _GroupRepository.GetGroupRepository(Idgroup);
    }
}

接口:

public interface IGroupService
{
    Task<Groups> GetGroupsService(int Idgroup);
    Task<bool> DeleteGroupService(int Idgroup);
}

public interface ISubGroupService
{
    Task<Subgroups> GetSubGroupService(int idsubgrupo);
    Task InsertSubgroupService(Subgroup subgroup);
}

在那种情况下你不能使用构造函数注入。可以切换到属性注入:

public class SubGroupService: ISubGroupService
{       
    private readonly ISubGroupRepository _SubGroupRepository;
    public IGroupService GroupService { get; set; }
    public SubGroupService(
        ISubGroupRepository SubGroupRepository)
    {          
        _SubGroupRepository = SubGroupRepository;
    }

    // methods of the class
}

public class GroupService : IGroupService
{
    public ISubGroupService SubGroupService {get; set;}
    private readonly IGroupRepository _GroupRepository;
    public GroupService(
        IGroupRepository GroupRepository)
    {
        _GroupRepository = GroupRepository;
    }
    
    // methods of the class
}

您必须像这样创建对象:

IGroupRepository groupRepository = new GroupRepository();
IGroupService groupService = new GroupService(groupRepository);
ISubGroupService subGroupService = new SubGroupService(groupRepository);
groupService.SubGroupSerivce = subGroupService;
subGroupService.GroupService = groupService;

当然,对象的创建现在要复杂得多。您可以将创建放入 facotry 方法中以避免错误:

public (IGroupService,ISubGroupService) CreateGroupAndSubGroupService()
{
   // code from above
}

并且还建议添加空值检查,因为有人可能会在没有正确初始化服务的情况下创建对象。