Search code examples
c#visual-studiorefactoringresharperautomated-refactoring

Visual Studio / Resharper Extract Interface to break god object


I'm trying to break a large object into smaller interfaces. Example:

public class SomeService
{
    readonly GodObject _god; // <-- Replace this with an Interface

    public SomeService(GodObject god)
    {
        _god = god;
    }

    public string SomeProperty => _god.GetSomeValue();
    public string SomethingElse => _god.SomethingDifferent;
}

public class GodObject
{
    public string GetSomeValue()
    {
        // do something and return
    }
    public string SomethingDifferent { get; set; }

    // Lots and lots more Members
}

After the refactoring it should look something like this:

public interface IPartialGodObject // <-- Extracted from GodObject
{
    string GetSomeValue();
    string SomethingDifferent { get; }
}

public class SomeService
{
    readonly IPartialGodObject _god; // <-- Now only references the Interface

    public SomeService(IPartialGodObject god)
    {
        _god = god;
    }

    public string SomeProperty => _god.GetSomeValue();
    public string SomethingElse => _god.SomethingDifferent;
}

public class GodObject : IPartialGodObject
{
    //...
}

Is Visual Studio or Resharper capable of doing something like this automated? I do know about Extract Interface, but with a large object it's quite painful to check only a few Properties/Methods from a large list.

Update: To clarify, I only want the members extracted which are used by the SomeService class.


Solution

  • It seems as there is no automated process to do this. What I ended up doing is the following (Resharper 2018):

    1. Extract Interface
    2. Select Public
    3. Replaced the member in question with the new interface
    4. Go to the interface and rely on Resharper's "Property is never used" to delete all unused properties