Is it normal for a DAL to have actual logic? For example, my Repository implementation is in the DAL layer (IRepository in BLL), if the data it needs isn't in the Local DB, it needs to scrape various websites to gather the data (and cache it into the DB). Is this normal? Or should the DAL just be a go between the DB?


Your Data Access Layer can have multiple objects in it.

In your case I would say you need as least two repositories

RepositoryDB : IRepository
RepositoryWebScraper : IRepository

You then have a choice about where to put the caching logic

CachedOnDBWebScraperRepository : IRepository

Which wraps both the others. or:

BusinessLogicService : IMyService

Which wraps both the repos and some other business logic

  • Like a decorator pattern? – keelerjr12 Mar 15 '18 at 13:26
  • hmm I think strictly speaking its not unless you forward all requests to the same underlying object. In this case only some of the request hit the WebScraper. – Ewan Mar 15 '18 at 13:35
  • But whatever gets scraped from the web needs to be entered in the database anyway. So query the DB, if the data is there then return data, if it isn’t then scrape the webpage, enter it into the database, and return the data. – keelerjr12 Mar 15 '18 at 19:37
  • yup. its v similar – Ewan Mar 15 '18 at 19:53

I would keep that logic out of the DAL and put it 1 layer above in the "business" or "controller" layer. That way your DAL just does "DAL" stuff.

Sample Pseudo Business Code:

        TOut obj;
        if (!_dal.Get(key, out obj))
            obj = _webscrape.Get(key);
            _dal.Set(key, obj)

        return obj;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.