I am using MVC3. I am getting following error when I run code analysis.
CA1506 : Microsoft.Maintainability : ‘MyController’ is coupled with 94
different types from 25 different namespaces. Rewrite or refactor this
class’s methods to decrease its class coupling, or consider moving
some of the class’s methods to some of the other types it is tightly
coupled with. A class coupling above 95 indicates poor
maintainability, a class coupling between 95 and 80 indicates moderate
maintainability, and a class coupling below 80 indicates good
maintainability.
This is a controller class.
May I know what is the best solution to decrease class coupling for a controller?
As mentioned by @musefan, by the sound of it you need to refactor. I’ve just had a look at a controller in a project I’m working with which I consider to be on the borderline of lacking cohesion and needing to be refactored, and it’s coupled to about 40 types.
Take another look at the controller and the area of your system it services, and see if you can split it up into fewer, more cohesive classes.
Edit
With relation to your Controller providing export functionality for Word, PDFs and Excel, I assume this means you have logic in your Controller which knows the details of putting together a Word export, a PDF export and an Excel export, including certain aspects common between the three formats which you’ve abstracted out (headers and footers).
If I’ve understood this correctly, one refactoring you could consider would be to move all the logic relating to headers, footers, structure and formatting into a different class behind an interface, and have your Controller reference that interface instead of referencing the various classes which manage those aspects. That would move coupling to various classes from your controller into the new document-handling class behind its interface, and perhaps drop the number of coupled classes to a level below that which FxCop finds acceptable.