c# - 此代码片段是否使用 Repository 或 Adapter 模式?

标签 c# design-patterns adapter repository-pattern

几天前我遇到了this blog post它提供了一个可插入的缓存管理器来使用不同的缓存提供者。基本上,我们有一个 ICacheProvider 接口(interface):

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

还有一个缓存管理器类:

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);
  }
}

最后,我们可以编写自己的提供程序:

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 (HttpContext.Current.Cache[key] != null)
      {
          item = (T)HttpContext.Current.Cache[key];
      }
      return item;
  }
}

好吧,我很确定这段代码使用了基于 http://www.dofactory.com/Patterns/PatternAdapter.aspx 中的定义的适配器模式。 .
但似乎我们也可以说它也使用 Repository 模式(除了它与通常使用 Repository 模式的数据的基本 CRUD 操作没有任何关系)。它在一个接口(interface)中包装了缓存管理器的一般内容。

我们可以说这段代码使用了存储库模式适配器模式吗?

最佳答案

我认为是的,因为这不是存储库。

存储库是领域对象的集合,它设法将领域业务转换为从业务本身抽象出来的另一种事物。

换句话说,我再说一遍,这不是存储库。

关于c# - 此代码片段是否使用 Repository 或 Adapter 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4781482/

相关文章:

javascript - jQuery(反)模式 : building selectors with string manipulation

design-patterns - 两种模型 View 设计以及模型之间的通信

java - 对象对其他对象了解多少?是违反得墨忒耳定律的例子吗?

java - Android 中文本的适配器

c# - 在vb.net或c#中将curl转换为webrequest

c# - 无法从传输连接读取数据 :An existing connection was forcibly closed by the remote host

c# - asp.net 网站中的 7-zip 压缩

c# - 允许用户内联编写脚本,.net 或 java 有哪些内联脚本引擎?

android - 从适配器访问 fragment 变量

Java动态适配器