0

I have a system where I want to start introducing some 2nd level caching. I want to abstract data access a bit and allow some type of other class to do the dirty work of fetching a DTO; it will check cache first and only go to database if it has to. Also, it might do some additional work on the DTO before passing it back, like applying some business rules, etc. Would this be considered the Proxy, Facade, or "Manager" design pattern? Here is an example:

class UserDTO {
   int Id {get;set;}
   string UserName {get;set;}     
}

class UserXXX {   // <<< What is this called?  Proxy, Facade, Manager?
   UserDTO GetUser(int userId) {
      UserDTO user;

      //get user: check cache first
      string cacheKey = "user_" + userId.ToString(); 
      if (cache.Exists(cacheKey)){
          user = cache.Get(cacheKey);
      } else{
          user = db.Users.Find(userId);
          //add user to cache for next time
          cache.Add(cacheKey, user);
      }

      //possibly do something else with user here,
      //like authorization check or some other business logic

      return user;
   }
}
Brady Holt
  • 109
  • 3
  • 1
    see [Pattern for caching DAOs: strategy or decorator?](http://programmers.stackexchange.com/questions/267304/pattern-for-caching-daos-strategy-or-decorator) – gnat Mar 19 '15 at 21:23
  • The word you're looking for is "cache". – Doval Mar 19 '15 at 22:03
  • @Doval I don't want to name it cache because from a consumer's perspective, it shouldn't know how the User is being fetched. I might have a UserXXX that uses cache but I might have PermissionXXX that does not check cache and hits db everytime. In effects, the XXX is a wrapper of sorts and handles dirty work of getting the DTO(s). – Brady Holt Mar 19 '15 at 22:05
  • There is a gigantic difference between not having to know how something gets done and not having any clue what's going on. You can't really ignore where the user is coming from. If it's cached, what's the caching policy? How outdated can the cache's data get? Can getting the thing fail because of some internal validation? Will this thing return `null` or some default Null Object if it can't find the user? All of those details matter even if you wish you could ignore them. – Doval Mar 19 '15 at 22:25

0 Answers0