【问题标题】:Visual Studio / Resharper Extract Interface to break god objectVisual Studio / Resharper 提取接口以打破上帝对象
【发布时间】:2018-08-02 14:46:07
【问题描述】:

我正在尝试将大对象分解为较小的接口。示例:

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
}

重构之后应该是这样的:

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
{
    //...
}

Visual Studio 或 Resharper 是否能够自动执行此类操作?我确实知道Extract Interface,但是对于一个大对象,从一个大列表中只检查几个属性/方法是非常痛苦的。

更新:澄清一下,我只想提取SomeService 类使用的成员。

【问题讨论】:

    标签: c# visual-studio refactoring resharper automated-refactoring


    【解决方案1】:

    似乎没有自动流程来执行此操作。我最终做的是以下(Resharper 2018):

    1. Extract Interface
    2. Select Public
    3. 用新界面替换了有问题的成员
    4. 进入界面,依靠Resharper的“Property is never used”删除所有未使用的属性

    【讨论】:

      猜你喜欢
      • 2013-01-24
      • 2021-11-05
      • 2015-11-07
      • 2011-07-29
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 2013-03-30
      • 1970-01-01
      相关资源
      最近更新 更多