From the course: Building and Securing RESTful APIs in ASP.NET Core

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Move data access to a service

Move data access to a service - ASP.NET Core Tutorial

From the course: Building and Securing RESTful APIs in ASP.NET Core

Start my 1-month free trial

Move data access to a service

- [Instructor] Right now the controller is directly interacting with the data context. It's a better pattern to move this data access code to a service class and keep the controller as thin as possible. By relying on a service to interact with the dbcontext, the data access concerns are separated from the controller. This also has the benefit of making the controller easier to test because you can inject a mock service and run tests without actually interacting with the database. To start writing the service layer for this API, I'll create a new folder here on the root called Services. And in here I'll create one new service interface called IRoomService. I'll make this public. And right now this interface will just have one method. It'll be a task that returns a room, let's import that model's namespace. And we'll call this GetRoomAsync, given a Guid id. Now I'll create my implementation, also here in Services, a class called, you can either call this roomservice or I like to say…

Contents