Geeks With Blogs

  • JaySmith @phatboyg Awesome, I wonder how many java developers look at VS12 and go wow, that looks like Eclipse, or other Java IDE ::::: MENU ::::: about 1024 days ago
  • JaySmith @bmsullivan Oh yeah, I do see that little bit of green on the run in debug icon. Just seems like there was more in the beta. about 1024 days ago
  • JaySmith @bmsullivan I only see color on the RSharper icon on the toolbar, and solution explorer has none at all. I am using the dark theme. about 1024 days ago
  • JaySmith #VS2012 Where did the color in the icons go that was in VS11 Beta???? about 1024 days ago

Jay Smith Failing my way to success!

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.



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


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.


Posted on Wednesday, February 27, 2008 7:56 PM .NET , Visual Studio | Back to top

Comments on this post: Refactor: Visual Studio Extract Interface

Comments are closed.
Comments have been closed on this topic.
Copyright © Jay Smith | Powered by: | Join free