开发者

Does this code snippet use Repository or Adapter pattern?

A few days ago I came across this blog post which provides a pluggable cache manager to use different cache providers. basically, we have an ICacheProvider Interface:

public interface ICacheProvider
{
  void Store(string key, object data);
  void Destroy(string key);
  T Get<T>(string key);
}

And a CacheManager Class:

public class CacheManager
{
  protected ICacheProvider _repository;
  public CacheManager(ICacheProvider repository)
  {
      _repository = repository;
  }

  public void Store(string key, object data)
  {
      _repository.Store(key, data);
  }

  public void Destroy(string key)
  {
      _repository.Destroy(key);
  }

  public T Get<T>(string key)
  {
      return _repository.Get<T>(key);
  }
}

And finally, we can write our own provider:

public class SessionProvider : ICacheProvider
{
  public void Store(string key, object data)
  {
      HttpContext.Current.Cache.Insert(key, data);
  }

  public void Destroy(string key)
  {
      HttpContext.Current.Cache.Remove(key);
  }

  public T Get<T>(string key)
  {
      T item = default(T);
      if (Ht开发者_JAVA技巧tpContext.Current.Cache[key] != null)
      {
          item = (T)HttpContext.Current.Cache[key];
      }
      return item;
  }
}

Well, I'm pretty sure this code uses Adapter Pattern based on the definition at http://www.dofactory.com/Patterns/PatternAdapter.aspx.

But it seems like we can say it uses Repository pattern as well (Except it doesn't have anything to do with basic CRUD operations on data which is normally where the Repository pattern is used). It wraps up the general stuff for the cache manager in an interface.

Can we say that this code uses Repository pattern and adapter pattern?


I think so, because this isn't a repository.

Repository is a collection of domain objects which manages to translate domain business into another thing abstracted from the business itself.

In other words, I repeat, this isn't repository.


It looks like a repository pattern.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜