MVC Controller Best Practices

MVC Controller Best Practices

Here are a few basic best practice guidelines to keep in mind when writing your controllers using the MVC pattern with ASP.NET MVC.
  • Keep controllers “skinny, keep action methods small
    • Your controller actions should be kept as small as possible, with business logic living in your models.  “Fat Model, Skinny Controller” is the motto. Here is a weird reminder of that  😯
  • Use an Inversion of Control (IoC) container (i.e. Spring, Ninject, etc) to manage dependencies
    • IoC is a must. It will make testing your code SOOOO much easier. You want to minimize any new object instantiations and instead defer to your IoC container to handle that for you. It’s just one less thing to worry about in your app. Looking for an IoC container? .NET Guru Scott Hanselman has put together a list of some IoC containers that he has used.
  • Do not rely on magic strings, but instead rely on strongly-typed views 
    • Magic strings are bad. It doesn’t mean they should never ever be used, but it’s a lot of times tough to argue for them when you have the compile time safety of a strongly typed view. Magic strings don’t lend themselves as much to easy refactoring.
    • Stay away from  ( ViewData[“myKey]) or ( ViewBag.MyKey=”myKey”) in your controllers as much as possible. Although I do believe TempData has it’s place, that is another topic most likely…
  • Use your action verbs (Post/Get)

These are just a few quick best practices to help you write better controllers with ASP.NET MVC. Any other helpful controller best practices I am missing?

Share Your Thoughts!

Testimonials

What My Clients Say

Karin - Hope and Future

Brent has always been very prompt and generally exceeds our expectations with his work. His maintenance fee is very reasonable. I strongly recommend him!  We get many compliments on our beautiful web site!

Dan

I contacted Krueger Web Design and Brent did everything.  He setup a test site with the changes so we could verify everything was correct before we transferred from my old hosting provider.  During the setup and conversion Brent was extremely professional and responsive. He responded to any issues the same day.

Dan Kaufman
Emily - GFCLT

We've had a great experience working with Krueger Web Design. Prompt responses, great ideas, effective work... all at a reasonable price! This was the perfect fit for our organization.

Mahmud - ACMC

I would love to recommend you to anyone! You have been very helpful when we needed you the most.

Brian - SEMNIC

Working with Krueger Web Design was effortless. Communicating what I wanted for the website and the results that I saw were identical. The customer service was always prompt and I am completely satisfied with the product.

Zach - Stadium Club

Exactly what I wanted! Fast turnaround time and very affordable. Answered all of our questions quickly and made adjustments promptly. Very easy to work with and puts everything into terminology that even I can understand!

Contact Krueger Web Design

Have a question for me? Need an estimate on some work? You can email me at brent@kruegerwebdesign.com or drop me a line using the button below:

Copyright © 2017 Krueger Web Design - All rights reserved
Web Design in Madison, WI