英文:
Avoid Two Services Calling Each Other
问题
Here's the modified code after resolving the circular dependency issue by using constructor injection and creating an interface for one of the services:
public class UserService : IUserService
{
private readonly ReviewCustomersContext _context;
private readonly IMapper _mapper;
private readonly IRequestService _requestService;
private readonly IReviewService _reviewService;
private readonly IDocumentService _documentService;
private readonly IDeleteRequestService _deleteRequestService;
public UserService(ReviewCustomersContext context, IMapper mapper, IRequestService requestService, IReviewService reviewService,
IDocumentService documentService, IDeleteRequestService deleteRequestService)
{
_context = context;
_mapper = mapper;
_requestService = requestService;
_reviewService = reviewService;
_documentService = documentService;
_deleteRequestService = deleteRequestService;
}
// Add your UserService methods here
}
public interface IRequestService
{
// Declare the methods provided by RequestService
}
public class RequestService : IRequestService
{
private readonly ReviewCustomersContext _context;
private readonly IUserService _userService;
private readonly IMapper _mapper;
public RequestService(ReviewCustomersContext context, IUserService userService, IMapper mapper)
{
_context = context;
_userService = userService;
_mapper = mapper;
}
// Add your RequestService methods here
}
By creating an interface IRequestService
, you can now inject IRequestService
into UserService
without causing a circular dependency. You can implement the methods of IRequestService
in the RequestService
class. This design adheres to the principles of dependency injection and should resolve the circular dependency issue.
Please let me know if you need any further modifications or if you have any other questions.
英文:
I'm trying to create an API using dependency injection where I have two services calling each other.
public class UserService : IUserService
{
private readonly ReviewCustomersContext _context;
private readonly IMapper _mapper;
private readonly IRequestService _requestService;
private readonly IReviewService _reviewService;
private readonly IDocumentService _documentService;
private readonly IDeleteRequestService _deleteRequestService;
public UserService(ReviewCustomersContext context, IMapper mapper, IRequestService requestService, IReviewService reviewService,
IDocumentService documentService, IDeleteRequestService deleteRequestService)
{
_context = context;
_mapper = mapper;
_requestService = requestService;
_reviewService = reviewService;
_documentService = documentService;
_deleteRequestService = deleteRequestService;
}
and
public class RequestService : IRequestService
{
private readonly ReviewCustomersContext _context;
private readonly IUserService _userService;
private readonly IMapper _mapper;
public RequestService(ReviewCustomersContext context, IUserService userService, IMapper mapper)
{
_context = context;
_userService = userService;
_mapper = mapper;
}
I get error messages about the fact that UserService
is calling RequestService
and RequestService
is calling UserService
.
Is there any solution to solve that except duplicate the code in the functions I use to a third class?
答案1
得分: 1
一种方法是不使用构造函数注入,而是使用属性,因此将不起作用的代码:
public class ServiceA : IServiceA
{
private IServiceB _serviceB;
public ServiceA(IServiceB serviceB)
{
_service = serviceB;
}
}
public class ServiceB : IServiceB
{
private readonly IServiceA _serviceA;
public ServiceB(IServiceA serviceA)
{
_serviceA = serviceA;
}
}
改为类似以下的方式:
public class ServiceA : IServiceA
{
private IServiceB _serviceB;
public ServiceA()
{
}
public IServiceB ServiceB
{
get { return _serviceB; }
set { _serviceB = value; }
}
}
public class ServiceB : IServiceB
{
private readonly IServiceA _serviceA;
public ServiceB(IServiceA serviceA)
{
_serviceA = serviceA;
serviceA.ServiceB = this;
}
}
英文:
One way is to not use constructor injection but a property instead, so changing
public class ServiceA : IServiceA
{
private IServiceB _serviceB;
public ServiceA(IServiceB serviceB)
{
_service = serviceB;
}
public class ServiceB : IServiceB
{
private readonly IServiceA _serviceA;
public ServiceB(IServiceA serviceA)
{
_serviceA = serviceA;
}
}
which doesn't work, into something like
public class ServiceA : IServiceA
{
private IServiceB _serviceB;
public ServiceA()
{
}
public IServiceB
{
get { return _serviceB; }
set { _serviceB = value; }
}
}
public class ServiceB : IServiceB
{
private readonly IServiceA _serviceA;
public ServiceB(IServiceA serviceA)
{
_serviceA = serviceA;
serviceA.ServiceB = this;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论