Design a site like this with
Get started

Fat Controllers: Going on a Diet

After watching a training video I learn something new that will make me a better developer.

In my quest to be a better developer, I watched a video in LinkedIn Learning called ASP.NET MVC: Building for Productivity and Maintainability by Jess Chadwick and was impressed with what he taught. One of the main topics I got from this is that my controllers are fat. Very fat. In fact, they need to go on a diet big time.

My controllers had everything including the kitchen sink in them. It worked but it was getting hard to follow the logic flow. Even worse, if I had to come back to do maintenance it took a while to relearn what I was doing. Jess taught me how to separate business logic from the controller using services.

Why hadn’t I been doing this stuff from the beginning? Well, simply put, I wasn’t taught to write code that way. Besides my Computer Science classes in college, I was self-taught on c# and the .NET MVC Framework. All those tutorials on video or books never covered the best practices in coding. Now, after watching this video, I know better.

I had a project that I am working on that had a couple of reports that felt was a good candidate to try out this system of services. I figured out how to wire it up and create the service provider and then the corresponding service. Then created the method in the controller to use the service.

I already had a fat method from one report so I converted that to use a service. Now the controller has two methods with about ten lines each that are easy to follow. Nice and slim and all the business logic separated in a service. I really like this system.

Another benefit in using services is Unit testing. It will be a lot easier to do a Unit test against the services rather than against a fat controller. I tried learning to do Unit testing against my fat controller and I ended up giving up. I couldn’t figure out how to do it but now, with services, I can see it will be a lot easier to do.

If I had time, I would redo all of the controllers for my project but unfortunately, I don’t. I’m not giving up. Any chance I have I’ll continue to put my fat controllers on the diet. Any new project and I will definitely be using the new method. I feel I have progressed as a developer and that is good.

Has anyone else seen the video? If so, tell me what you learned from it.

Happy coding everyone!