Extracting Interfaces

To better understand the Extract Interface operation, let’s look at an example. Suppose you review your code and notice that a number of your domain objects share similar properties and methods. Let’s say the objects Invoice, Order, and Product all contain properties for Id and Name and methods for Save and Delete. In this case, you should consider extracting this commonality into a standard interface that each of your domain objects would implement. Let’s look at how the Extract Interface refactoring operation aids in this regard.

First, you position your cursor on the target class whose members you want to extract. In the example, choose the Invoice class and position the cursor on the class name. Then press Ctrl+. to show the light bulb. From here choose Extract Interface. Invoking the Extract Interface operation presents a dialog box named the same. Figure 9.20 shows this dialog box relative to the example.

Image

FIGURE 9.20 Use Extract Interface to create an interface based on an existing class.

Notice that you first define a name for the interface. By default, the tool names the interface with the name of the class preceded by the letter I for interface (in this case, IInvoice). Of course, we are going to use our interface across our domain, so we change this to IBusinessEntity.

The Extract Interface dialog box also shows the generated name and the new filename for the interface. The generated name is simply the fully qualified name of the interface. This is used by the class for implementation of the interface. The New File Name text box shows the filename for the interface. All extracted interfaces result in the creation of a new file. The tool tries to keep the filename in sync with the interface name.

The last thing to do is select which members of the object you want to publish as an interface. Of course, only public members are displayed in this list. For this example, select the members Delete, Id, Name, and Save.

Clicking the OK button generates the interface. The only change that is made to the Invoice class is that it now implements the new interface, as in the following line of code.

public class Invoice : IBusinessEntity

The interface is then extracted to a new file. Listing 9.4 shows the newly extracted interface.

LISTING 9.4 The Extracted Interface


namespace Contoso.Fx.Common
{
  interface IBusinessEntity
  {
    int Id { get; set; }
    string Name { get; set; }
    void Delete();
    void Save();
  }
}


The next step in the example is to go out to each domain object and implement the new interface. This is not exactly refactoring, but Visual Studio does help make this easier. Once you indicate that the given object implements an interface, Visual Studio pops up the light bulb in the code editor. This helps implement the interface. Figure 9.21 shows the light bulb that results from typing IBusinessEntity after the Order class declaration.

Image

FIGURE 9.21 Implementing an interface with the help of the light bulb.

Notice in Figure 9.21 that you have two options: Implement Interface and Implement Interface Explicitly. The former checks the current class to see whether there are implementations that apply. The latter generates code that explicitly calls the interface items. It puts all this code inside a region for the given interface. This capability can be useful if you’re stubbing out a new class based on the interface. The following lines of code provide an example of explicitly implementing the Save method of the interface.

void IBusinessEntity.Save() {
  throw new NotImplementedException();
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset