扩展方法可能是要走的路,但我认为这有点像让你的逻辑成为编译时单例,如果它永远不会改变,那很好。
所以我会尝试这样的事情,因为从字面上看,一切都是基于合同和可注射的
public class Poco
{
public int Foo;
public int Bar;
}
public class PocoUtility : IPocoUtility
{
private IService _service;
public PocoUtility(IService service)
{
_service = service;
}
public Result DoSomethingWithPoco(Poco poco)
{
return _service.Convert(poco);
}
}
public class SomethingThatUsesAPoco
{
private IPocoUtility _logicForSomething;
public SomethingThatUsesAPoco(IPocoFactory pocoFactory, IPocoUtility logicForSomething)
{
_poco = pocoFactory.CreateInstance();
}
public Result DoIt()
{
return _logicForSomething.DoSomethingWithPoco(_poco);
}
}