Move Extracted Interfaces to Their Own File using ReSharper

Posted almost 3 years ago on February 06, 2009

Update 2/6/09:

Thanks for @orangy for pointing out that there is an even more streamlined approach that I overlooked.

With your cursor on the concrete class use the "Refactor This" keyboard shortcut (CTRL+SHIFT+R), then X.  The extract interface dialog pops. 

Then use the keyboard shortcut ALT+F to change the radio button to "Place in another file".  Then tab through the rest of the controls and make your selections.

Much nicer.

 

One thing I do commonly when developing is create a class, and then extract an interface from it.  ReSharper makes that easy:

1) Put your cursor on the concrete class name.

2) Use ReSharper's "Refactor This" keyboard shortcut (CTRL+SHIFT+R).

3) Hit "x" on the keyboard for "extract interface".

A keyboard friendly dialog will open which will allow you to select which members get extracted out into the interface. 

So now you have an interface sitting in the same file as your concrete class.  That's perfectly acceptable, but I always prefer having each class/interface in it's own file named the same as the class/interface name.  To do that, you just need to put your cursor on the interface name and hit "CTRL+Enter, Enter", to select the option for "Move to another file to match type name". 

Thanks to www.twitter.com/orangy for helping me find this shortcut!!

moveinterface

 

This is part 6 of a series of posts exploring how to become a ReSharper keyboard ninja.

Previously:
Part 5 - Find the Next Error Using ReSharper
Part 4 - What To Pass?
Part 3 - Surround Your Code
Part 2 - Find Inheritors
Part 1 - Quick Documentation View

Comments

Jon Kruger writes...

This ReSharper addin will allow you to move classes and interfaces to any project in your solution (which can help when doing TDD):

http://code.google.com/p/resharper-tdd-productivity-plugin/

February 06, 2009

Ilya Ryzhenkov writes...

Hmm, don't you see radio buttons about where to place extracted interface right there, in refactoring dialog? :)

February 06, 2009

Steve Horn writes...

@Ilya Ryzhenkov

I do see that now. The reason I didn't think there was a keyboard shortcut for the "Place in new file" option was because the underlines for the shortcuts don't show up on the labels until you hit ALT on the keyboard.

Thanks!!

February 06, 2009

danshultz writes...

I like too that it makes the interface public by default.

nice anim btw.

February 06, 2009

New Comment