Visual Studio / Resharper提取接口可破坏对象

时间:2018-08-02 14:46:08

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

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

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类使用的成员。

1 个答案:

答案 0 :(得分:0)

似乎没有自动执行的操作。我最终要做的是以下内容( Resharper 2018 ):

  1. select count(columnname) from TABLEB failing : Error: java.io.IOException: java.io.IOException: java.lang.RuntimeException: java.lang.ClassCastException:org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector cannot be cast to org.apache.hadoop.hive.ql.exec.vector.LongColumnVector
  2. Extract Interface
  3. 用新界面替换了有问题的成员
  4. 转到界面并依靠 Resharper的“从不使用属性” 删除所有未使用的属性