views:

509

answers:

7

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?

+3  A: 

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.

Chris Shaffer
+1  A: 

DRY is not specific to any one technology. Just make sure you look at your classes from a functionality standpoint (not even from a copy/paste coder view) and see where the duplication occurs. This process will probably not happen in one sitting, and you will only notice duplication after reviewing your code several months later when adding a new feature. If you have unit tests, you should have no fear in removing that duplication.

casademora
+1  A: 

Don't Repeat Yourself. It can apply to many different aspects of programming. The most basic level of this is prevent code smell. I haven't used ASP.NET so I can't get specific to it and MVC's.

  • In C++ Templating prevets multiple copies of the same function.
  • In C void * pointers can be used in a similar fashion, but with great care.
  • Inheriting from another function allows function allows other functions to use the same code base without having to copy the code.
  • Normalizing data in a database minimizes redundant data. Also adhereing to the DRY principle.

When you go over a "thought" in a project. Ask yourself.

  1. Have I already wrote this code?
  2. Will this code be useful elsewhere.
  3. Can I save coding by building off of a previous class/function.
J.J.
+1  A: 

One advantage of MVC as related to not repeating yourself is that your controller can do tasks common to all pages in the one class. For example, validating against certain types of malicious requests or validating authentication can be centralized.

Greg Ogle
A: 
  • 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
Matt Hinze
A: 

DRY should not only be applied to code, but to information in general. Are you repeating things in your build system? Do you have data which should be moved to a common configuration file, etc.

JesperE
A: 

Well, the most common example that I can give about DRY and UI is using things like MasterPages and UserControls.

MasterPages ensure that you have written all the static HTML only once.

UserControls ensure reusability of code. Example, you will have a lot of forms doing basic stuff like CRUD. Now, ideally we want all users to see different pages for Create and Update though the forms fields in both will almost be the same. What we can do is combine all the common controls and put them into a control that can be reused over both the pages. This ensures that we are never retyping (or copy-pasting) the same code.

DRY is especially important in MVC because of the increase in the sheer number of files to accomplish the same task.

Adhip Gupta