A QUICK PATTERN EXAMPLE- Refactoring to Principles – Continuation Part 5

The Final Problem
HTTP context is tightly coupled with Product Service class. suppose consider in future, HTTP Context has been changed to MemCached.. we need to modify our product Service class!! How can we avoid this…??

Adapter Pattern

yes!!! by employing Adapter Pattern , we can be able to resolve this pattern

Adapter Pattern : Basically Converts the interface of a class into another interface clients expect… let us see the implementation now…!!

First let us abstract HTTP Context from Product Service class (By implementing Interface).

Create an interface ICacheStorage as shown below

public interface ICacheStorage
    {
        void Remove(string key);
        void Add(string key,object data);
        T Retreive<T>(string key);
    }

Implement this interface in Product Service class

commented code is before implemenation

 public class ProductService
    {
        private IProductRepository _productRepository;
        private ICacheStorage _cacheStorage;

        //public ProductService(IProductRepository productRepository )
        //{
        //    _productRepository = productRepository;
        //}

        public ProductService(IProductRepository productRepository,ICacheStorage cacheStorage)
        {
            _productRepository = productRepository;
            _cacheStorage = cacheStorage;
        }


        public IList<Product> GetAllProductsIn(int categoryId)
        {
            IList<Product> products;
            string storageKey = string.Format("Products_in_category_id_{0}", categoryId);


           
            //products = (List<Product>)HttpContext.Current.Cache.Get(storageKey);
            products = _cacheStorage.Retreive<List<Product>>(storageKey);
            
            if (products == null)
            {
                products = _productRepository.GetAllProductsIn(categoryId);

               // HttpContext.Current.Cache.Insert(storageKey,products);
                _cacheStorage.Add(storageKey,products);
            }
            return products;

        }

    }

Now Abstraction completed.. i.e we have removed dependency of HTTPContext from Product Service class.. 🙂

Now we need to make HTTPContext to implement our ICacheStorage interface… can it implement directly.. nope.. so we need to have a way so that HTTPContext will implement our ICacheStorage.. that is where Adapter Pattern comes in to Picture

Adapter Pattern UML diagrams
Adapter Pattern UML

Adapter Pattern UML

Adapter Pattern UML for our use case

Adapter Pattern UML for our Use Case

let us now implement….

create a class HttpContextCacheAdapter which is a wrapper for HTTP Context cache.


 public class HttpContextCacheAdapter: ICacheStorage
    {
        public void Remove(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        public void Add(string key, object data)
        {
            HttpContext.Current.Cache.Insert(key, data);
        }

        public T Retreive<T>(string key)
        {
            T itemStored =(T) HttpContext.Current.Cache.Get(key);
            if (itemStored == null)
                itemStored = default(T);
            return itemStored;
        }
    }

What have we achieved by implementing this Adapter pattern…??

let us suppose if we need to implement another cache service like MemCached or velocity,all you would need to do is create an Adapter that allows
the ProductService class to interact with the caching storage provider via the common ICacheStorage
interface

finally our code is clean no refactoring for now !!!….at least i can see that… 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s