views:

738

answers:

10

Simple question - As I get more and more namespaces in my solution, the list of using statements at the top of my files grows longer and longer. This is especially the case in my unit tests where for each component that might be called I need to include the using for the interface, the IoC container, and the concrete type. With upward of 17 lines of usings in my integration test files its just getting downright messy. Does anyone know if theres a way to define a macro for my base using statements? Any other solutions?

A: 

Resharper - the add-in for Visual Studio - has a feature that strips unsued Using's from a file, but I don't know anything that does quite what you describe.

Rik Garner
vs2008 does that too right click->organize usings->removed unused
George Mauer
Ahh, excellent. Haven't used 2008 yet.
Rik Garner
+4  A: 

Some people enjoy hiding the usings in a #region. Otherwise, I think you're out of luck. Unless you want to put the namespace on all your referents.

Blair Conrad
+1  A: 

Does anyone know if theres a way to define a macro for my base using statements?

Do you mean that namespaces you use often are automaticly added to each new class? If yes, Resharper can do that too. Additionaly it has a feature to put the usings in a region on code clean-up. Resharper may be the way to go (you won't regrett it as I can say from my own experience).

Roger Ween
+6  A: 

I know I shouldn't say this out loud, but, maybe reconsider your design.

17 usings in 1 file = a lot of coupling (on the namespace level).

Bjorn Reppen
I think he mentioned he's talking primarily about tests. I don't think you can avoid coupling your tests!
Peter Meyer
That is just silly. Take especially a unit test file for a component that references 3 other components. Its perfectly reasonable to have the following usings: 1 MbUnit, 1-2 rhino mocks, 1 IoC, for each component 1 for interface, 1 for implementation. 1 for each message class. And integration? Ugh
George Mauer
+2  A: 

Can't stand Resharper myself. But I also can't stand messy using statements. I use the Power Commands add-in for VS, which has a handy 'Remove and Sort' using statements command (among other good things).

Kent Boogaart
+1  A: 

VS2008 added an "Organize Usings" context menu, which has a Sort, Remove, and "Remove and Sort" option which will do what you want per file. The Visual Studio Power Commands add-in adds a context menu in the solution explorer for projects and solutions which is a "Remove and Sort" for all files in the project and all projects in the solution, respectively.

Scott Dorman
A: 

In VS2008, you can right click on the CS file and choose 'Organize Usings'. It will strip unused using and sort them for you too. Other than that, I would just use #region. Also, CTRL+M+O will collapse all your regions functions, etc at design time. I use this shortcut A LOT!

Bloodhound
Me too, but organizing usings is not my issue, my issue is that I use a lot of them!
George Mauer
+1  A: 

If you want to change the default using statements that are done when you create a new file, take a look in the C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033 directory. It contains a bunch of zip files that you can modify to change the templates for Code files (Obviously move up the directory structure to change other languages or other types of files).

See here for more information.

Matthew Steeples
+2  A: 

There are four possible problems here;

The namespaces in your code are dividing your classes too finely. if you have, for example;

using MyCompany.Drawing.Vector.Points;
using MyCompany.Drawing.Vector.Shapes;
using MyCompany.Drawing.Vector.Transformations;

consider collapsing them to the single MyCompany.Drawing.Vector namespace. You probably aren't gaining by dividing too much. Visual Studio Code Analysis/FxCop has a rule for this, checking the number of classes in a namespace. Too few and it will warn you.

You are putting too many tests into the same class. If you are referencing System.Data, System.Drawing, and System.IO in the same class, consider writing more atomic tests -- some which access databases, some which draw images, and some which access the file system. Then divide each type across three test classes.

You are writing tests which do too much. If you are referencing a lot of namespaces, your tests may be coupling too many features together. This kind of coupling can often be buggy, so try to break big, wide-ranging functions into smaller parts, and test these in seperate files.

Many are redundant. Are they all used, or are they just copy-pasted from other files. Right-click on the code editor and choose from the 'Organise Using' options to remove unused statements.

Steve Cooper
This is mostly a problem with integration tests that test 4, 6 or even more components at a time. So while you're correct that I'm testing too much, such is the nature of integration tests.
George Mauer
+1  A: 

It may help to use aliasing. Not sure it it's worth it, but instead of:

using System.Web.UI;
using System.Web.Mail;
using System.Web.Security;
... Control ...
... MailMessage ...
... Roles ...

you can use:

using W = System.Web;
... W.UI.Control ...
... W.Mail.MailMessage ...
... W.Security.Rolse ...
Eugene Katz