From the course: ASP.NET MVC: Building for Productivity and Maintainability

Unlock the full course today

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

Consuming a service from a controller

Consuming a service from a controller

From the course: ASP.NET MVC: Building for Productivity and Maintainability

Start my 1-month free trial

Consuming a service from a controller

- [Instructor] Now that I've created the service, the first thing I'll do is register it with the dependency injection framework, so I can inject it wherever I need it. I'll register it using the same register type call that I used for the data context. Including creating only one instance per request. And then it's finally time to wrap this all up, by updating the controller and replacing all that current business logic with a call to this new service. First I'll inject the service into the controller's constructor, just like I did with the DB context. Then I'll remove all of the logic that's been moved out of the update method, and replace it with a call to the service. Note that I'm leaving the model state validation calls at the beginning of the method, even though the controller is technically no longer responsible for validating the object, since the update product request properties are still adorned with the validation attributes, this will actually continue to work just fine,…

Contents