Note that there are some explanatory texts on larger screens.

plurals
  1. POWhich part of model should handle database inserts?
    text
    copied!<p>Title probably doesn't describe my problem too well, I'll be glad if somebody could edit it to something more appropriate. Anyways:</p> <p>I got a component that is supposed to return product price given its <code>id</code>. It implements an interface like this one:</p> <pre><code>interface IProductPriceFetcher { double GetPrice(int id); } </code></pre> <p>Now, the price can be fetched from <strong>3 different</strong> sources:</p> <ul> <li>web service</li> <li>directly from website source code (scrapping)</li> <li>as a final fallback (both webservice and website are inaccessible), most recent price from local database is returned</li> </ul> <p>To play around this 3-different-sources issue I've implemented class like this:</p> <pre><code>class MainFetcher : IProductPriceFetcher { public double GetPrice(int id) { var priceFetcher = this.factory.GetWebServiceFetcher() ?? this.factory.GetWebsiteFetcher() ?? this.factory.GetLocalDatabaseFetcher(); return priceFetcher.GetPrice(id); } } </code></pre> <p>Each method from factory returns <code>IProductPriceFetcher</code> of course, with side note that first two can fail and return <code>null</code>; I assumed <code>GetLocalDatabaseFetcher</code> will always return meaningful object tho.</p> <h3>My "general wondering... ment"</h3> <p>Upon successful webservice/website call, I want fetched price to be inserted into local datbase, as a future fallback case. Now my question is: which part of code above should be responsible for that? Should it be one of the concrete web fetchers that returns price? Or "aggregator" fetcher (<code>MainFetcher</code>), as it also has knowledge over what's the source of price? Should I raise some event? Inject yet another interface with DB calls? Change design to better?</p> <p>Why did it even occur as an issue to me? Well, I tried to keep the code clean (worry not, it's only pet project for my spare time - exactly for solving problems like this) possibly with SRP/SoC in mind. Now I seem to have problems switching from this mindset - I mean, how could possibly something that fetches webpages also be doing database inserts? Oh, come on! :)</p>
 

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