You are using inheritance - reidev275/SignsYouShouldProbablyRefactor GitHub Wiki

Example

class WebRequestor
{
 public WebResponse IssueWebRequest(WebRequest request) { ... }
}

class TwitterRepository : WebRequestor
{
 public IEnumerable<Tweet> GetTweets()
 {
   var request = new WebRequest();
   var response = IssueWebRequest(request);
   return response.Data;
 }
}

Why is this bad?

  1. From TwitterRepository the context of IssueWebRequest is not apparent. This is drastically intensified by multiple levels of inheritance.
  2. Any change to WebRequestor explicitly changes TwitterRepository.
  3. We'd prefer to use composition over inheritance.

Resolved

By using constructor injection we can give TwitterRepository the ability to issue web requests without it having to be a WebRequestor.

interface IWebRequestor
{
  public WebResponse IssueWebRequest(WebRequest request) { ... }
}

class TwitterRepository
{
  readonly IWebRequestor _webRequestor;
  public TwitterRepository(IWebRequestor webRequestor)
  {
    _webRequestor = webRequestor;
  }

  public IEnumerable<Tweet> GetTweets()
  {
    var request = new WebRequest();
    var response = _webRequestor.IssueWebRequest(request);
    return response.Data;
  }
}
⚠️ **GitHub.com Fallback** ⚠️