locked
Use application service instead of controllers RRS feed

  • Question

  • User-637833180 posted

    Hi,

    I have my application services and my controllers. All coordination login and everything else is inside Application services. Controllers are just there to call app services and pass parameters. I find this redundant as I have over 20 controllers. Is there any way to get rid of the controllers and whenever resolve routes directly to application services. check my code below:

    public class MyAppService : IMyAppService
    {
       public List<ClientDto> GetClients()
       {
         ....some logic here
       }

       public void CreateClient(Client input)
       {
         ....some logic here
       }
    }

    [Route("[controller]/[action]")]
    [ApiController]
    public class MyController : Controller
    {
        private readonly IMyAppService myAppService;

        public MyController(IMyAppService myAppService)
        {
          this.myAppService = myAppService;
        }

        public ActionResult<List<ClientDto>> GetClients()
        {
           return this.myAppService.GetClients();
        }

        public void CreateClient(Client input)
        {
           this.myAppService.CreateClient(input);
        }
    }

    Monday, September 23, 2019 6:04 AM

All replies

  • User1120430333 posted

    Nope, you have to have a controller with action methods that are routed to in a controller, becuase  ASP.NET WebAPI follows the MVC pattern which can have views too as the WebAPI link below the first one shows.

    https://hub.packtpub.com/working-aspnet-web-api/

    <copied>

    The ASP.NET Web API architecture

    The ASP.NET Web API is a lightweight web-based architecture that uses HTTP as the application protocol. Routing in the ASP.NET Web API works a bit differently compared to the way it works in ASP.NET MVC. The basic difference between routing in MVC and routing in a Web API is that, Web API uses the HTTP method, and not the URI path, to select the action. The Web API Framework uses a routing table to determine which action is to be invoked for a particular request. You need to specify the routing parameters in the WebApiConfig.cs file that resides in the App_Start directory.

    <end>

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-1

    Monday, September 23, 2019 6:51 AM
  • User-637833180 posted

    So there isn't any way to do this? I understand the reason of the controllers but I am sure I have seen somewhere the same concept. and when I said replace I mean to have HttpContext in service not replace the "Controller" just replace the redundancy I have in my code.

    Monday, September 23, 2019 8:48 AM
  • User1120430333 posted

    So there isn't any way to do this? I understand the reason of the controllers but I am sure I have seen somewhere the same concept. and when I said replace I mean to have HttpContext in service not replace the "Controller" just replace the redundancy I have in my code.

    What redudency? All I see is that you are using Seperation of Concerns and DI  of an object into another object and calling methods on the injected object. Is this a DLL that you are calling a service?

    https://en.wikipedia.org/wiki/Separation_of_concerns

    Monday, September 23, 2019 10:13 AM
  • User475983607 posted

    So there isn't any way to do this? I understand the reason of the controllers but I am sure I have seen somewhere the same concept. and when I said replace I mean to have HttpContext in service not replace the "Controller" just replace the redundancy I have in my code.

    API controller actions are where you get to run code and they define the API I/O - the interface.  This is help when using API documentation APIs like swagger.  The client applications can use a code generator.

    I suspect you followed a repository pattern?  And the redundancy is CRUD operation?  Can you explain the redundancy?

    Monday, September 23, 2019 12:05 PM