Posted by: Cirilo Meggiolaro | 01/16/2009

Tip of the day #94 – Refactoring – Extracting an Interface

Let’s check today a nice feature from Visual Studio 2008 from the refactor feature: Generating an interface from an existing class.

For this example, we are going to use the following class:

Picture 1 - The sample class used to extract an interface.

Picture 1 - The sample class used to extract an interface.

How to…

  • To extract an interface, right-click anywhere in the class and select Refactor > Extract Interface;
Picture 2 - Refactoring options on the context menu.

Picture 2 - Refactoring options on the context menu.

  • The Extract Interface window is displayed. Enter the interface name, the file name, select the members that form the interface and click ok;
Picture 3 - The Extract Interface Window

Picture 3 - The Extract Interface Window

  • The interface is generated;
Picture 4 - The generated interface

Picture 4 - The generated interface

Picture 5 - Interface file in Solution Explorer Window.

Picture 5 - Interface file in Solution Explorer Window.

  • The original class has its signature changed to implement automatically the interface;
Picture 5 - The original class is implementing interface.

Picture 6 - The original class is implementing interface.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: