Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to have a super-decoupled design, I would implement a <a href="http://www.dofactory.com/Patterns/PatternDecorator.aspx" rel="nofollow">Decorator</a> class like the following and use it to wrap both the WebServiceFetcher and the WebsiteFetcher:</p> <pre><code>class DatabaseCachingFetcherDecorator : IProductPriceFetcher { private readonly IProductPriceFetcher innerFetcher; public DatabaseCachingFetcherDecorator(IProductPriceFetcher fetcher) { this.innerFetcher = fetcher; } public double GetPrice(int id) { double price = this.innerFetcher.GetPrice(id); if (price != 0) // or some other value representing "price not found" { SavePriceToDatabase(id, price); } return price; } private SavePriceToDatabase(int id, double price) { // TODO: Implement... } } </code></pre> <p>Then your factory would implement the following methods:</p> <pre><code>public IProductPriceFetcher GetWebServiceFetcher() { return new DatabaseCachingFetcherDecorator(new WebServiceFetcher()); } public IProductPriceFetcher GetWebsiteFetcher() { return new DatabaseCachingFetcherDecorator(new WebsiteFetcher()); } </code></pre> <p>This design decouples your actual fetchers from your caching mechanism.</p> <p><strong>EDIT:</strong> I misread your design slightly with this answer, as I assumed that the GetPrice method would return some sort of NULL value if the price couldn't be fetched, rather than the factory returning a NULL value. I think the factory returning NULL smells a bit, since a factory's responsibility is to reliably return objects. I would consider changing your <code>GetPrice</code> method interface to return a <code>double?</code> perhaps, to allow for "price not found".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload