Elijah Manor
Elijah Manor

Reputation: 18023

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?

Upvotes: 5

Views: 5051

Answers (8)

John
John

Reputation: 1

There seems to be a misconception that everything in a domain model has to be copied up as a special view model. You can have domain models be domain models but view models be something that know nothing of domain specifics and be more generic. For example:

Domain Model classes: Account, Asset, PurchaseOrder

View Model: List, Table, Tuple, SearchFormBackingModel:Checked options, Outputoptions, etc. The view itself might be much more view implementation specific.

The Tuple/Dictonary/Map might map to Account, Asset and PurchaseOrder single instances but a Table might be useful for a collection of them etc. You still have MVC but you have session data, not ready for transaction yet in a view model without necessarily having it violate the rules of your domain model which is where the rules should go. They will be less anemic and anti-pattern that way. You can pass these rules up front and use them there or just in back or both depending on how the system reads from clients etc.

Upvotes: 0

Adhip Gupta
Adhip Gupta

Reputation: 7163

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.

Upvotes: 0

JesperE
JesperE

Reputation: 64414

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.

Upvotes: 0

Matt Hinze
Matt Hinze

Reputation: 13679

  • 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

Upvotes: 4

oglester
oglester

Reputation: 6655

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.

Upvotes: 1

J.J.
J.J.

Reputation: 4872

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.

Upvotes: 2

casademora
casademora

Reputation: 69667

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.

Upvotes: 1

Chris Shaffer
Chris Shaffer

Reputation: 32575

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.

Upvotes: 7

Related Questions