Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5931643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:43:25+00:00 2026-05-22T14:43:25+00:00

Title probably doesn’t describe my problem too well, I’ll be glad if somebody could

  • 0

Title probably doesn’t describe my problem too well, I’ll be glad if somebody could edit it to something more appropriate. Anyways:

I got a component that is supposed to return product price given its id. It implements an interface like this one:

interface IProductPriceFetcher
{
    double GetPrice(int id);
}

Now, the price can be fetched from 3 different sources:

  • web service
  • directly from website source code (scrapping)
  • as a final fallback (both webservice and website are inaccessible), most recent price from local database is returned

To play around this 3-different-sources issue I’ve implemented class like this:

class MainFetcher : IProductPriceFetcher
{
    public double GetPrice(int id)
    {
        var priceFetcher = this.factory.GetWebServiceFetcher()
                        ?? this.factory.GetWebsiteFetcher()
                        ?? this.factory.GetLocalDatabaseFetcher();
        return priceFetcher.GetPrice(id);
    }
}

Each method from factory returns IProductPriceFetcher of course, with side note that first two can fail and return null; I assumed GetLocalDatabaseFetcher will always return meaningful object tho.

My “general wondering… ment”

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 (MainFetcher), 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?

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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:43:26+00:00Added an answer on May 22, 2026 at 2:43 pm

    If you want to have a super-decoupled design, I would implement a Decorator class like the following and use it to wrap both the WebServiceFetcher and the WebsiteFetcher:

    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...
        }
    }
    

    Then your factory would implement the following methods:

    public IProductPriceFetcher GetWebServiceFetcher()
    {
        return new DatabaseCachingFetcherDecorator(new WebServiceFetcher());
    }
    
    public IProductPriceFetcher GetWebsiteFetcher()
    {
        return new DatabaseCachingFetcherDecorator(new WebsiteFetcher());
    }
    

    This design decouples your actual fetchers from your caching mechanism.

    EDIT: 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 GetPrice method interface to return a double? perhaps, to allow for “price not found”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My title is probably not really describing the problem real well. I do not
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
Title probably doesn't make a lot of sense, so I'll start with some code:
The title probably doesn't make much sense, so I'll try to be descriptive here
I'm probably misunderstanding JSON, but why this code doesn't work? HTML <html> <head> <title>Test</title>
That title probably doesn't sound right, but forgive me, I'm learning Rails for the
Sorry, that title probably doesn't make much sense, but what I want to know
I probably couldn't depict it better in the title (and if it has a
Title more or less says it all. Specifically, I've become increasingly annoyed that in
Alright, probably not the best title, but meh. I have the following code inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.