C# + ReSharper = Awesome: Tip #6 – Extract Interface

This is the sixth in a series of quick how-to articles on ReSharper.

Tip #6 – Extract Interface

Use: Creates a new interface based on the selected class and updates the class to implement the new interface. This is most useful when working with an existing code base because we all define our interfaces first when doing greenfield development, right?  Smile

Before
   1:      public class Shooter
   2:      {
   3:          public string Name { get; set; }
   4:   
   5:          public string ReleaseDate { get; set; }
   6:   
   7:          public int MaxPlayers { get; set; }
   8:   
   9:          public bool HasZombies { get; set; }
  10:   
  11:          public bool IsHalo { get; set; }
  12:   
  13:          public void Borrow()
  14:          {
  15:              //TODO: Implement borrow logic.
  16:          }
  17:      }
Right-click the class

image

Select members

SNAGHTML4614fae1

After
   1:      public interface IGame
   2:      {
   3:          string Name { get; set; }
   4:          string ReleaseDate { get; set; }
   5:          int MaxPlayers { get; set; }
   6:          void Borrow();
   7:      }
   8:   
   9:      public class Shooter : IGame
  10:      {
  11:          public string Name { get; set; }
  12:   
  13:          public string ReleaseDate { get; set; }
  14:   
  15:          public int MaxPlayers { get; set; }
  16:   
  17:          public bool HasZombies { get; set; }
  18:   
  19:          public bool IsHalo { get; set; }
  20:   
  21:          public void Borrow()
  22:          {
  23:              //TODO: Implement borrow logic.
  24:          }
  25:      }

Happy coding!

 

C# + ReSharper = Awesome: Tip #5 – Replace Constructor with Factory Method

This is the fifth in a series of quick how-to articles on ReSharper.

Tip #5 – Replace Constructor with Factory Method

Use: If an application must control how or when new instances of classes are created, a factory method can achieve this. ReSharper makes it simple to wrap a constructor with a static factory method.

Before
   1:      public class Car
   2:      {
   3:          private IList<IPart> _parts;
   4:   
   5:           public Car(IList<IPart> parts)
   6:           {
   7:               _parts = parts;
   8:           }
   9:      }
Right-click the constructor

image

Name your method or accept the default

SNAGHTML1ecbd865

After
   1:      public class Car
   2:      {
   3:          public static Car CreateCar(IList<IPart> parts)
   4:          {
   5:              return new Car(parts);
   6:          }
   7:   
   8:          private IList<IPart> _parts;
   9:   
  10:          private Car(IList<IPart> parts)
  11:           {
  12:               _parts = parts;
  13:           }
  14:      }

 

Happy coding!

 

C# + ReSharper = Awesome: Tip #4 – Convert Abstract Class to Interface

This is the fourth in a series of quick how-to articles on ReSharper.

Tip #4 – Convert Abstract Class to Interface

Use: This is used when the class(es) that will be inheriting from a base class also need to inherit from another base class. Derived types can inherit from only one base class but can implement multiple interfaces.

Before
   1:      public abstract class Book
   2:      {
   3:          public abstract string Title { get; set; }
   4:   
   5:          public abstract string Year { get; set; }
   6:   
   7:          public abstract string Author { get; set; }
   8:   
   9:          public abstract void Lend();
  10:   
  11:          public abstract void AddToInventory();
  12:      }
Right-click the class

image

After
   1:      public interface Book
   2:      {
   3:          string Title { get; set; }
   4:          string Year { get; set; }
   5:          string Author { get; set; }
   6:          void Lend();
   7:          void AddToInventory();
   8:      }

Note: Notice that this refactoring does not change the name of the type. At this point, there will be a squiggly line under the interface’s name, Book. Placing the cursor on Book and pressing <Alt+Enter> will prompt ReSharper to rename it to IBook.

 

Happy coding!

 

Mastodon
github.com/alvinashcraft