2

我正在编写一个管理智能应用程序,它需要大量复杂的数据库查询,其中一些查询非常昂贵。为了提高性能,我大量使用 Memcached 来尽可能多地存储在内存中。

这导致我的代码中有很多重复,我渴望摆脱这些重复并构建一个更清洁的数据访问解决方案。我的很多数据访问功能最终看起来像这样..

public int NumberOfTimeouts(DateTime date, int? applicationId)
{
    var functionCacheKey = "NumberOfTimeouts";
    var cacheKey = string.Format("{0}-{1}-{2}-{3}", RepositoryCacheKey, functionCacheKey, date, applicationId);
    var cachedNumberTimeouts = _cache.Retrieve(cacheKey);
    if (cachedNumberTimeouts != null)
    {
        return (int)cachedNumberTimeouts;
    }

    //query logic here, calculates numberOfTimeouts

    UpdateCache(date, cacheKey, numberOfTimeouts);
    return numberOfTimeouts;
}

我只是不太确定标准方法是什么,它是否涉及使用自定义属性类或类似的东西?

4

2 回答 2

1

这是一个跨领域的问题。该Decorator模式可能适用于此。我可能对这种模式没有经验,但我会试一试

// model
public class CustomObject
{
    public int Id { get; set; }
}
// interface
public interface IRepository<T>
{
    IEnumerable<T> Find(Expression<Func<T, bool>> expression);
}
public interface ICacheableRepository<T>
{
    IEnumerable<T> Find(Expression<Func<T, bool>> expression, Func<int> cacheKey);
}
public interface IRepositoryCacheManager<T>
{
    IEnumerable<T> Get(int key);
    bool Any(int key);
    void Add(int key, IEnumerable<T> result);
}
// cache manager
public class RepositoryCacheManager<T> : IRepositoryCacheManager<T>
{
    private Dictionary<int, IEnumerable<T>> cache = new Dictionary<int,IEnumerable<T>>();
    #region IRepositoryCache<T> Members

    public IEnumerable<T> Get(int key)
    {
        return cache[key];
    }

    public bool Any(int key)
    {
        IEnumerable<T> result = null;
        return cache.TryGetValue(key, out result);
    }

    public void Add(int key, IEnumerable<T> result)
    {
        cache.Add(key, result);
    }

    #endregion
}

// cache repository decorator
public class CachedRepositoryDecorator<T> : IRepository<T>, ICacheableRepository<T>
{
    public CachedRepositoryDecorator(IRepositoryCacheManager<T> cache
        , IRepository<T> member)
    {
        this.member = member;
        this.cache = cache;
    }

    private IRepository<T> member;
    private IRepositoryCacheManager<T> cache;

    #region IRepository<T> Members

    // this is not caching
    public IEnumerable<T> Find(Expression<Func<T, bool>> expression)
    {
        return member.Find(expression);
    }

    #endregion

    #region ICacheableRepository<T> Members

    public IEnumerable<T> Find(Expression<Func<T, bool>> expression, Func<int> cacheKey)
    {
        if (cache.Any(cacheKey()))
        {
            return cache.Get(cacheKey());
        }
        else
        {
            IEnumerable<T> result = member.Find(expression);
            cache.Add(cacheKey(), result);
            return result;
        }
    }

    #endregion
}
// object repository
public class CustomObjectRepository : IRepository<CustomObject>
{
    #region IRepository<CustomObject> Members

    public IEnumerable<CustomObject> Find(Expression<Func<CustomObject, bool>> expression)
    {
        List<CustomObject> cust = new List<CustomObject>();
        // retrieve data here
        return cust;
    }

    #endregion
}
// example
public class Consumer
{
    // this cache manager should be persistent, maybe can be used in static, etc
    IRepositoryCacheManager<CustomObject> cache = new RepositoryCacheManager<CustomObject>();
    public Consumer()
    {
        int id = 25;

        ICacheableRepository<CustomObject> customObjectRepository =
            new CachedRepositoryDecorator<CustomObject>(
                cache
                , new CustomObjectRepository()
                );
        customObjectRepository.Find(k => k.Id == id, () => { return id; });
    }
}

请注意:

  • 我没有测试过这段代码,不知道它是否功能齐全。我只是描述插图
  • ICacheableRepository是的,通过重载 for ,这有代码气味Find,但是我无法使用Expressionas KeyinDictionary

优点:

  • 此 CachedRepositoryDe​​corator 可用于任何通用存储库(可重用)
  • select进程内部没有缓存逻辑,强调一下SRP

缺点:

  • 没有 ORM 很难实现,也许你需要一些反射调整才能让它在没有 ORM 的情况下工作
  • 一开始很难理解
  • 没有 DI 容器很难接线

归功于这篇文章:)

于 2013-07-05T09:52:19.797 回答
0

我认为工作单元模式是您所需要的。更多信息:

或者包含 UoW 模式的框架:https ://github.com/riteshrao/ncommon

于 2013-07-05T09:15:12.767 回答