DDD存储库可以知道用户上下文吗? [英] Can DDD repositories be aware of user context?

查看:66
本文介绍了DDD存储库可以知道用户上下文吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您要开发一个系统,该系统的实体和域逻辑的可用性高度依赖于用户上下文。通过使单个存储库实例具有用户上下文意识来处理存储库中的用户上下文敏感性是否有意义?我正在考虑采用这种方法,将对用户上下文的依赖从我的实体上移开,但是我不确定在朝这个方向前进时是否存在我可能不知道的陷阱。我打算首先解决的方法是将UserContext参数添加到需要此上下文信息的存储库的构造函数中。另一个明显的选择是将用户上下文信息输入到我的存储库中的每个查询方法中,但这可能意味着所有方法中的大多数都将需要此类参数,这反过来会大大增加每个方法调用的详细程度。



我还要指出,我知道即使我要使存储库用户上下文都知道,这在服务或实体需要同一用户时不一定直接有帮助上下文信息之类的原因,例如根据用户配置确定行为。我也对这些情况下的其他解决方案感兴趣,但现在我正尝试一次解决一件事,因此我首先关注存储库。



解决方案

我在这里感觉到设计的味道:-)。事物到达领域层时,应该将它们转换成领域实体/属性,并且不应该依赖上下文。我的意思是应该使用上下文来更改/表示实体的新状态。在这里,似乎上下文将用于确定实体的持久化方式。



我已经说过了,如果您对上下文的依赖性更多地是从基础结构角度而不是从业务功能角度而言,那么拥有上下文相关存储库是正确的模型



为此,您是否可以考虑像Spring在Hibernate Session中那样通过本地线程传递usercontext?这样,您的Repository类的构造函数或方法将受到较少的污染。但是,它确实会降低代码的可读性。



希望有帮助。


Say you were to develop a system which availability of entities and domain logic is highly dependent on user context. Would it make sense to handle the user context sensitivity within repositories by making individual repository instances user context aware? I'm considering adopting this methodology as a way pulling the reliance on user context away from my Entities but I'm not sure whether there are any pitfalls that I may not be aware of with going this direction. The way I'm planning approaching this first is to add a UserContext parameter to the constructors of repositories that need this context information. The other obvious option would be to feed user context information into each query method in my repositories but this would likely mean that the majority of all methods would require such parameter which would in turn greatly increase the verbosity of each method call.

Also I would like to point out that I'm aware that even if I am to make repositories user context aware that this does not necessarily help directly when a service or entity needs that same user context information for reasons such as determining behavior based on user configuration. I'm interested in other solutions for these cases as well but for now I'm trying to tackle one thing at a time so I'm focusing on the repositories first.

Any suggestions would be appreciated.

解决方案

I sense a design smell here :-). Things by the time they reach domain layer should pretty much be translated into domain entities/attributes and should not have dependency on context. What I mean is the context should be used to change/represent the new state of entity. Here it more seems like that context is going to be used to determine how the entity is going to be persisted. Have I understood this correctly?

Having said that, if your dependency on context is more from an infrastructure perspective rather than business functionality perspective then having context sensitive Repositories is the right model you have come up with.

Towards that, could you consider passing usercontext through thread local like Spring does with Hibernate Session? This way your Repository class's constructors or methods would be less polluted. It, however, does reduce the readablility of your code a bit.

Hope that helps.

这篇关于DDD存储库可以知道用户上下文吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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