Visual Studio / Resharper Extract Interface to break god object
Visual Studio / Resharper Extract Interface to break god object
我正在尝试将一个大对象分解成更小的界面。示例:
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
,但是对于一个大对象来说,从一个大列表中只检查几个 Properties/Methods 是非常痛苦的。
Update:澄清一下,我只想提取 SomeService
class.
使用的成员
似乎没有自动化流程可以执行此操作。我最终做的是以下 (Resharper 2018):
Extract Interface
Select Public
- 用新界面替换了有问题的成员
- 进入界面,依靠Resharper的"Property is never used"删除所有不用的属性
我正在尝试将一个大对象分解成更小的界面。示例:
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
,但是对于一个大对象来说,从一个大列表中只检查几个 Properties/Methods 是非常痛苦的。
Update:澄清一下,我只想提取 SomeService
class.
似乎没有自动化流程可以执行此操作。我最终做的是以下 (Resharper 2018):
Extract Interface
Select Public
- 用新界面替换了有问题的成员
- 进入界面,依靠Resharper的"Property is never used"删除所有不用的属性