Visual Studio Extract Interface Command

Interfaces define a contract for classes or structures. This helps to support polymorphism in object-oriented programming and assist automated testing when used for mocking. Visual Studio lets you to create interfaces from existing classes automatically.

Interfaces

There are many reasons to use interfaces when using object-oriented programming techniques. Originally, interfaces were designed to provide a means for supporting polymorphism by enforcing a contract for all classes that implement the interface. Variables and method parameters that use the interface type provide polymorphic behaviour by allowing values of any type that support the interface to be assigned to them.

The increase of tools for automated testing, particularly mocking frameworks, extend the use of interfaces. In these cases, it is common for interfaces to be defined that may only be implemented by a single class in the production code. The interface is also used in the automated test code to generate mock objects that mimic the behaviour of real objects and support the interface in an easily controlled manner. This often requires that an existing class's members be extracted into a new interface.

Extracting an Interface

If you have an existing class or structure that contains methods and properties that you would like to add to a new interface, you can use Visual Studio's extract interface command to create the interface automatically. The command generates a new code file containing the interface's definition and modifies the source class so that it implements the new interface. You can use the command on existing interfaces too, to extract some of its members and create an inheritance hierarchy of interfaces

The above code includes properties and methods that may be useful for all types of two-dimensional shape. As such, you may decide to create an interface named "IShape" that contains the Width, Height, Area and Perimeter members. This can then be implemented by the Rectangle class and any other shape class that you may wish to define. It can also be used as the basis for mocking.

To extract the interface, right-click within the class definition to display a context-sensitive menu. From this menu, select "Refactor", then "Extract Interface..." The Extract Interface dialog box will be displayed.

The dialog box requires that you specify three pieces of information:

New interface name. Enter the name for the new interface into this text box. As you type, the generated name will be updated. This name will be added to the list containing any base class and interfaces already implemented by the source class.

New file name. The extracted interface will be created in a new file. This textbox is used to specify the name for that file.

Select public members to form interface. All of the public members of the source class are listed in this box. If you wish to extract all of the members to the new interface, click the Select All button. Alternatively, check each item that you wish to be generated in the interface and uncheck those that you do not require.

Once you have provided the necessary information, you should click the OK button to execute the command. In this case, enter "IShape" for the interface name and select all of the members before clicking OK. A new file containing the following interface will be generated and this interface will be implemented by the Rectangle class: