I am designing a class that uses an abstract property to provide a point of access to a field. Here is a snippet of my code:
public abstract class PageBroker : WebBroker { public abstract IPageProvider Provider { get; } } public class ArticleBroker : PageBroker { public override IPageProvider Provider { get; } = new ArticleProvider(); } I realized I could refactor this to use DI instead, like so:
public class PageBroker : WebBroker { public PageBroker(IPageProvider provider) { this.Provider = provider; } public IPageProvider Provider { get; private set; } // implementation... } // no derived class, just new PageBroker(new ArticleProvider()) In what situation(s) would one technique be appropriate over the other?
abstractso a derived class can implement it, or pass it in through the constructor. This has nothing to do with setter injection.IPageProvider(pretty much the definition of DI). The use for the first is if I had another justification thatArticleBrokerneeds to differ/inherit from aPageBroker.