我应该在 DDD 应用程序中的哪一层实现数据库查找数据的缓存?
我正在使用 DDD 设计 WCF 服务。 我有一个域服务层,它调用存储库来创建域对象。该存储库是使用 ADO.Net 而不是 ORM 实现的。数据来自使用存储过程的数据库。当创建一个对象时,比如说一个地址,SP 返回一个状态 ID。 SP不会将地址表与状态表连接起来。状态由具有 id、abbr 和 name 属性的值对象类 State 表示。当应用程序启动时,状态对象列表可以被缓存(使用 system.runtime.caching.memorycache),因为它是非易失性数据。一般来说,我有一个 LookupDataRepository 可以从表中检索所有此类查找数据。现在,AddressRepository 必须从州 ID 填充地址的 State 属性。
伪代码:
class AddressRepository : IAddressRepository
{
Address GetAddressById(int id)
{
// call sp and map from data reader
Address addr = new Address(id);
addr.Line = rdr.GetString(1);
addr.State = // what to do ?, ideally LookupCache.GetState(rdr.GetInt32(2))
}
}
class State
{
public int Id;
public string Abbr;
public string Name;
enum StateId {VIC, NSW, WA, SA};
public static State Victoria = // what to do, ideally LookupCache.GetState(StateId.VIC)
}
// then somewhere in address domain model
if(currentState = State.Victroia)
{
// specific logic for Victoria
}
我的问题是把这个缓存放在哪一层?服务、存储库、跨所有层可用的单独程序集。
I am designing a WCF service using DDD.
I have a domain service layer that calls repository to create domain objects. The repository is implemented using ADO.Net and not an ORM. The data comes from DB using Stored Procs. While creating an object say an Address the SP returns an id for state. The SP will not join address table with the state table. The state is represented by a value object class State that has id, abbr and name properties. The list of state objects can be cached (using system.runtime.caching.memorycache) when the application starts up as it is non-volatile data. In general I have a LookupDataRepository that can retrieve all such lookup data from tables. Now the AddressRepository has to populate the State property of address from the state id.
pseudo code:
class AddressRepository : IAddressRepository
{
Address GetAddressById(int id)
{
// call sp and map from data reader
Address addr = new Address(id);
addr.Line = rdr.GetString(1);
addr.State = // what to do ?, ideally LookupCache.GetState(rdr.GetInt32(2))
}
}
class State
{
public int Id;
public string Abbr;
public string Name;
enum StateId {VIC, NSW, WA, SA};
public static State Victoria = // what to do, ideally LookupCache.GetState(StateId.VIC)
}
// then somewhere in address domain model
if(currentState = State.Victroia)
{
// specific logic for Victoria
}
My question is which layer to put this cache ?. Service, Repository, a separate assembly available across all layers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
缓存放在哪里?这取决于。
如果您的场景是将您的
IAddressRepository
注入多个应用程序服务(我相信您称之为域服务),结果将是:我会在服务层进行缓存。如果感觉更自然,并且可以让您更好地控制要缓存的位置和时间。存储库级别通常是低粒度的。服务层及其方法更接近用例,这样您就知道何时缓存以及缓存什么内容。
我真的建议编写一个缓存包装器,例如
它保存对 System.Runtime.Caching.MemoryCache.Default 的静态引用。
它使您的缓存类型安全,并且转换仅在包装器内部完成。您还可以使用注入的模拟
ICacheManager
来对您的服务进行单元测试。更高级的方法是使用面向方面的编程和装饰器/拦截器来做到这一点。 Stack Overflow 上有大量有用的信息。
Where to put Cache? It depends.
If you're scenario will be that you inject your
IAddressRepository
into several application services (I believe you call 'em Domain services) the outcome will be:I would go for caching at Service layer. If feels more natural and gives you more control of where and when you want to cache. Repository level is usually to low grained. Service layer and its methods is more closer to use cases and it's then you know when and what to cache.
I really recommend writing a cache wrapper like
That holds a static reference to
System.Runtime.Caching.MemoryCache.Default
.It makes your Caching type safety and casting is only done inside wrapper. You can also unit test your services with a Mocked
ICacheManager
injected.A more advanced approach is to do this with Aspect Oriented Programming and decorators/interceptors. You have tons of good info here at Stack Overflow.
根据我的观点和我的经验,缓存作为存储库会导致重复代码、增加域的复杂性以及所有事件都需要缓存吗?由于将实现 IRepository 接口......
所以我选择了基础设施服务,让我们只缓存应用程序中需要的内容。它也可供所有其他希望使用它的服务使用。
In my opinion and from my experience, would caching as a repository result in duplicate code, more complexity to the domain, and all events need to be cached? Since an IRepository interface would be implemented ...
So I opted for an infrastructure service, let's cache only what is needed in our app. It is also available to all other services that wish to consume it.