Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Does the DRY Principle Actually Look Like in ASP.NET MVC?

I keep hearing about the DRY Principle and how it is so important in ASP.NET MVC, but when I do research on Google I don't seem to quite understand exactly how it applies to MVC.

From what I've read its not really the copy & paste code smell, which I thought it was, but it is more than that.

Can any of you give some insight into how I might use the DRY Principle in my ASP.NET MVC application?

like image 460
Elijah Manor Avatar asked Oct 09 '08 15:10

Elijah Manor


2 Answers

DRY just means "Don't Repeat Yourself". Make sure that when you write code, you only write it one time. If you find yourself writing similar functionality in all of your Controller classes, make a base controller class that has the functionality and then inherit from it, or move the functionality into another class and call it from there instead of repeating it in all the controllers.

like image 188
Chris Shaffer Avatar answered Oct 04 '22 01:10

Chris Shaffer


  • use filter attributes to manage aspects (authentication, navigation, breadcrumbs, etc)
  • use a layer supertype controller (apply common controller-level filters to it, see mvccontrib for an example)
  • write custom actionresults (like in mvccontrib - for example we made one called logoutresult that just does a FormsAuthentication.Logout()
  • use a convention for view names
  • most importantly - keep you controller actions dumb, look for reuse opportunities in services
like image 38
Matt Hinze Avatar answered Oct 04 '22 03:10

Matt Hinze