3 ms·
> Using DI is just a glorified way to have "globals". How so? > Wanna unit test? Pass in a modified function giving back the time you want. Right. And all D
by lusr 14y ago
> Using DI is just a glorified way to have "globals".
How so?
> Wanna unit test? Pass in a modified function giving back the time you want.
Right. And all DI does is make it possible for the following constructor to be invoked without special knowledge or adverse coupling:
interface IArticleService {
void Publish();
}
class ArticleService : IArticleService {
protected IArticleRepository ArticleRepository;
protected ITimeService TimeService;
public ArticleService(IArticleRepository articleRepository, ITimeService timeService) {
ArticleRepository = articleRepository;
TimeService = timeService;
}
public void Publish(int id) {
ArticleRepository.Publish(id, TimeService.Now);
}
}