0

I'm in the school of thought of 'thin controllers', and love to push logic down into services and the domain models. However, I'm wondering if my controller should be making an HTTP Request to another web service or if the application service should do it. It seems that with my school of thinking then "yes" I should push it into the application service. However, this means the application service is stuck with HTTP only. So if I want to use a different communication protocol I'll need to implement another service like RabbitMQService instead of HttpService.

For example:

public Controller {
 // POST api/controller 
 [HttpPost]
 public async Task PostAsync([FromBody] Dictionary<string, int> data)
 {
 // get data from input json
 var output = service.doWork();
 // prep output data for outgoing HTTP Request
 // this here or in a service like 'HttpService'?
 await _sender.PostAsync(host + "/api/xxxxx", data);
 }
}

Or have the application service send the PostAsync request?

asked Mar 29, 2019 at 11:42
8
  • it would be helpful if you fleshed out your example with some details on what exactly the functions are supposed to do Commented Mar 29, 2019 at 12:02
  • @Ewan, I know the example is very abstract but I've added [slightly] more to it. Unfortunately, I cannot post the exact code. I do appreciate your answer below! Commented Mar 29, 2019 at 12:07
  • what I really need is the name of the function that you are calling on the httpservice. it doesn thave to be the real name. you can see im forced to use "DoWorkAndNextStep" it should be SendEmail or SaveContact or whatever Commented Mar 29, 2019 at 12:11
  • I use ElectionService to retrieve a Election by Id like electionService.GetElection(electionId). And then use Election to vote for a candidate with election.Vote(candidateId);. After the vote is recorded, I make another HTTP request to the candidates. This is not very RESTful and not the way I'd ever design/develop an application. But it's a demonstration in making API calls between web services. Commented Mar 29, 2019 at 12:15
  • 1
    No. Only handles the incomming requests to your system, not the calls/handling to the externals. That's someonelse problem. Commented Mar 29, 2019 at 14:15

1 Answer 1

1

Hmm your Controller doesn't seem very 'thin' to me. I would have

public Controller {
 private IElectionService service; //possibly sends over http or Rabbit or whatever
 // POST api/controller 
 [HttpPost]
 public async Task Vote(string electionId, string candidateId)
 {
 await service.Vote(electionId, candidateId);
 }
}

We can imagine that the ElectionService has various injected services

public class ElectionService : IElectionService
{
 private IElectionRepository repo; //calls election api
 public async Task Vote(string electionId, string candidateId)
 {
 var election = repo.GetElection(electionId);
 if(election.Candidates.Includes(candidateId))
 {
 repo.AddVote(new Vote(electionId, candidateId));
 }
 else
 {
 throw an exception!
 }
 }
}
public ElectionRepository_Http : IElectionRepository {...}
public ElectionRepository_RabbitMQ : IElectionRepository {...}
etc
answered Mar 29, 2019 at 12:01
5
  • This is exactly how I feel that it should be done. Commented Mar 29, 2019 at 12:08
  • updated with your names Commented Mar 29, 2019 at 12:18
  • So the repository should be dealing with communication?? Or do you mean something like HttpElectionService : IElectionService? Commented Mar 29, 2019 at 12:29
  • 1
    you could do it all in the service I suppose. I just naturally separated it out Commented Mar 29, 2019 at 12:32
  • So the repository should be dealing with communication repository should deal with data sources. Whether they are connections or access to the file system is irrelevant to the ElectionService. That's the point of the repository abstraction Commented Mar 29, 2019 at 12:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.