在域驱动设计中缓存代码位置 [英] Caching Code Location in a Domain Driven Design

查看:155
本文介绍了在域驱动设计中缓存代码位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 处理DataBase的存储库访问

  2. 协调内容和值对象之间的交互的应用程序服务等。

你一般会把缓存代码消除一个昂贵的数据库调用?



我看到代码库只是缓存在整个地方,很难监控内存使用情况很难为其他开发人员提供指导。



请理解,我知道您只应该在需要时缓存数据,我只是问一个一般问题。 / p>

解决方案

关于抽象存储库的奇妙之处在于您可以使用装饰器模式来实现像缓存这样的交叉关切。



作为一个例子,给定一个IMyRepository接口,你可以像这样伪代码创建一个MyCachingRepository:

  public class MyCachingRepository:IMyRepository 
{
private readonly IMyRepository repository;

public MyCachingRepository(IMyRepository存储库)
{
if(repository == null)
{
throw new ArgumentNullException(repository);
}

this.repository = repository;
}

public Foo SelectFoo(int id)
{
Foo foo = ... //尝试从缓存获取foo

如果// foo不是缓存
{
foo = this.repository.SelectFoo(id);
//保存缓存中的foo
}
return foo;
}
}

在这个例子中,GetFoo由IMyRepository定义。请注意,如果缓存中没有找到该项目,那么只会调用装饰的Repository。



遵循单一可重复性原则,因为真正的缓存实现可以专注于检索和保存数据,而缓存装饰器可以专注于缓存。这可以让您彼此独立地变化。



在我们当前的项目中,我们使用了这种方法,什么是额外的好处是我们可以做一个事后的想法,没有甚至触摸原始的存储库。这是开放/封闭原则的精神。


In an application that has followed a Domain Driven Design where you have the following sorts of concepts

  1. A repository that deals with the DataBase access
  2. A application service that co-ordinates interactions between enties and value objects etc.

where in general would you put caching code to elimenate an expensive call to the database?

I have seen code bases that just cache all over the place and it is difficult to monitor memory usage and difficult to provide guidelines to other developers.

Please understand that I know you should only cache data when you need to, I am just asking a general question.

解决方案

The wonderful thing about abstract Repositories is that you can use the Decorator pattern to implement such cross-cutting concerns as caching.

As an example, given an IMyRepository interface, you can create a MyCachingRepository like this pseudo code:

public class MyCachingRepository : IMyRepository
{
    private readonly IMyRepository repository;

    public MyCachingRepository(IMyRepository repository)
    {
        if(repository == null)
        {
            throw new ArgumentNullException("repository");
        }

        this.repository = repository;
    }

    public Foo SelectFoo(int id)
    {    
        Foo foo = ... // attempt to get foo from cache

        if // foo is not it cache
        {
            foo = this.repository.SelectFoo(id);
            // save foo in cache
        }
        return foo;
    }
}

In this example, GetFoo is defined by IMyRepository. Notice how the decorated Repository is only invoked if the item isn't found by the cache.

This follows the Single Resposibility Principle because the real caching implementation can concentrate on retrieving and saving data, whereas the caching Decorator can concentrate on caching. This lets you vary the two independently of each other.

In our current project we used this approach, and what is extra nice is that we could do it as an afterthought without even touching the original Repositories. This is in spirit of the Open/Closed Principle.

这篇关于在域驱动设计中缓存代码位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆