You are using inheritance - reidev275/SignsYouShouldProbablyRefactor GitHub Wiki
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;
}
}
- From
TwitterRepository
the context ofIssueWebRequest
is not apparent. This is drastically intensified by multiple levels of inheritance. - Any change to
WebRequestor
explicitly changesTwitterRepository
. - We'd prefer to use composition over inheritance.
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;
}
}