text: ﻿(function () {
  
      "use strict";
  
      function fetch($http, $q, localStorageManager) {
  
          var self = this;
          self.$http = $http;
          self.$q = $q;
          self.localStorageManager = localStorageManager;
  
          self.inMemoryCache = {};
  
          self.fromCacheOrService = function (options) {
              var deferred = self.$q.defer();
              var cachedData = self.localStorageManager.get(self.getCacheKey(options));
              if (!cachedData) {
                  self.fromService(options).then(function (results) {
                      deferred.resolve(results);
                  }).catch(function (error) {
                      deferred.reject(error);
                  });
              } else {
                  deferred.resolve(cachedData.value);
              }
              return deferred.promise;
          };
  
          self.fromInMemoryCacheOrService = function (options) {
              var deferred = self.$q.defer();
  
              var cachedData = self.inMemoryCache[self.getCacheKey(options)];
  
              if (!cachedData) {
                  self.$http({ method: options.method, url: options.url, data: options.data, params: options.params }).then(function (results) {
                      self.inMemoryCache[self.getCacheKey(options)] = results;
                      deferred.resolve(results);
                  }).catch(function (error) {
                      deferred.reject(error);
                  });
              } else {
                  deferred.resolve(cachedData);
              }
              return deferred.promise;
          };
  
          self.fromService = function (options) {
              var deferred = self.$q.defer();
  
              self.$http({ method: options.method, url: options.url, data: options.data, params: options.params }).then(function (results) {
                  deferred.resolve(results);
              }).catch(function (error) {
                  deferred.reject(error);
              });
  
              return deferred.promise;
          };
  
          self.getCacheKey = function (options) {
              return options.key || options.url + JSON.stringify(options.params) + JSON.stringify(options.data);
          };
  
          self.invalidateCache = function (cacheKey) {
              //TODO:Implement
          };
  
          return self;
      }
  
      angular.module("app").service("fetch", ["$http","$q","localStorageManager",fetch]);
  
  })();