依赖注入中的循环引用



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

我的服务是:

SubGroupServiceGroupsService

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

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

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

SubGroupService

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;

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

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

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

相关内容

  • 没有找到相关文章

最新更新