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
From the course: ASP.NET MVC: Building for Productivity and Maintainability
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,…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Reducing duplicate code with custom action filters5m 49s
-
(Locked)
Creating explicit contracts with request objects6m 51s
-
(Locked)
Moving business logic from a controller to a service7m 10s
-
(Locked)
Consuming a service from a controller3m 11s
-
(Locked)
Challenge: Creating a model validation action filter2m 7s
-
(Locked)
Solution: Creating a model validation action filter3m 55s
-
(Locked)
-
-
-