Reputation: 509
I am architecting a WebAPI. In that WebAPI, I have the following structure using DI (IoC):
Controllers -> Services -> Repositorys -> EF : DB.
Now when a request can use multiple services what is best practice:
Controller -> ServiceOne/ServiceTwo -> Repositories
Where ServiceTwo is instantiated in ServiceOne's constructor (or) ServiceTwo and ServiceOne or both instantiated in Controller constructor.
I am trying to achieve separation of concerns and stuck on this implementation. I am also trying to accomplish good organization of code and less code duplication.
Thoughts?
Upvotes: 0
Views: 31
Reputation: 6566
I'm posting it as answer in addition to the comment as it exceed the lenght.
I'd say injecting serviceTwo into serviceOne would be good approach as controller does not have to deal with a lot of if elses to decide which service to go with. let the service layer followup with other services and conglomerate the data and return it to the controller.
If you have services injected within the services
Pros:
Cons:
If you have services injected into the controller
Pros:
Cons:
Upvotes: 1