Refactor: Visual Studio Extract Interface

I recently learned about a very cool option in Visual Studio, the ability to extract an Interface from an existing class.  This has made me much more comfortable in trying to adhere to a principle of not creating unnecessary code.  I often found myself thinking I should create an interface first before creating the concrete implementation even for objects that I didn't have any current plans for extending. 

This post will show you step by step how to extract an interface from an existing class, so keeping with open closed principle we can still extend the code base with our modifying the current object.

First lets consider the the Attendee class.

namespace Attendee { public class Attendee { private int _age; public int Age { get { return _age; } set { _age = value; } }

    private string \_firstname;
    public string FirstName
    {
        get { return \_firstname; }
        set { \_firstname = value; }
    }

    private string \_lastName;
    public string LastName
    {
        get { return \_lastName; }
        set { \_lastName = value; }
    }
    public bool Save()
    {
        return true;
    }
}

}

This is a very simple class implementation of an Attendee, a class I use in my Model-View-Controller presentation.  Note this is a very simple domain object with no logic.  To extract an interface place your cursor in the class name, Attendee in this case, select Extract Interface... on the Refactor menu.

ExtractInterface_01

This will launch the Extract Interface wizard.  We can now modify the name, file name and the members to include in the interface.

ExtractInterface_02

Clicking OK after making your selection will generate the interface below.

using System; namespace Attendee { interface IAttendee { int Age { get; set; } string FirstName { get; set; } string LastName { get; set; } bool Save(); } }

Now you are free to create a new implementation of the Attendee class with out having to change any of the code in the old Attendee class.  This has really reduced the fear of not creating an interface for everything first.  Of course I still do for most of my objects but if there is no reason to create it right now, this give me the confidence that I can defer creating the interface and extension point until it is necessary.

This article is part of the GWB Archives. Original Author: Jay Smith

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.