避免两个服务相互调用

huangapple go评论67阅读模式
英文:

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;
    }
}

huangapple
  • 本文由 发表于 2023年7月24日 18:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定