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); } }
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.
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.
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?
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?
Member
132 Points
344 Posts
Use application service instead of controllers
Sep 23, 2019 06:04 AM|pantonis|LINK
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);
}
}
Please mark this post as answer if it helped you solve your problem
Contributor
4963 Points
4218 Posts
Re: Use application service instead of controllers
Sep 23, 2019 06:51 AM|DA924|LINK
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
Member
132 Points
344 Posts
Re: Use application service instead of controllers
Sep 23, 2019 08:48 AM|pantonis|LINK
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.
Please mark this post as answer if it helped you solve your problem
Contributor
4963 Points
4218 Posts
Re: Use application service instead of controllers
Sep 23, 2019 10:13 AM|DA924|LINK
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
All-Star
53131 Points
23678 Posts
Re: Use application service instead of controllers
Sep 23, 2019 12:05 PM|mgebhard|LINK
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?