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?

  • it would be helpful if you fleshed out your example with some details on what exactly the functions are supposed to do – Ewan Mar 29 '19 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! – keelerjr12 Mar 29 '19 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 – Ewan Mar 29 '19 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. – keelerjr12 Mar 29 '19 at 12:15
  • 1
    No. Only handles the incomming requests to your system, not the calls/handling to the externals. That's someonelse problem. – Laiv Mar 29 '19 at 14:15
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
|improve this answer|||||
  • This is exactly how I feel that it should be done. – keelerjr12 Mar 29 '19 at 12:08
  • updated with your names – Ewan Mar 29 '19 at 12:18
  • So the repository should be dealing with communication?? Or do you mean something like HttpElectionService : IElectionService? – keelerjr12 Mar 29 '19 at 12:29
  • 1
    you could do it all in the service I suppose. I just naturally separated it out – Ewan Mar 29 '19 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 – Laiv Mar 29 '19 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.