DDD:存储库可以返回布尔值吗?

发布于 2024-12-05 05:18:17 字数 317 浏览 6 评论 0原文

存储库可以根据它(虚拟)包含的对象返回布尔值吗?

例如:

if (userRepository.checkCredentials(username, password))
{
    // ...

或者以详细的方式执行此操作是否是更好的方法:

user = userRepository.findByUsername(username);
if (user != null && user.checkPassword(password)) {
{
    // ...

Is it ok for a Repository to return boolean values based on objects it (virtually) contains?

For example:

if (userRepository.checkCredentials(username, password))
{
    // ...

Or is it a better approach to do it the verbose way:

user = userRepository.findByUsername(username);
if (user != null && user.checkPassword(password)) {
{
    // ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月染过的梦 2024-12-12 05:18:17

听起来更像是一个规范,不是吗?

if (canAuthenticateSpec.isSatisfiedBy(username, password))

过去,我通过规范来完成此操作,因此我可以清楚、简单地进行检查以供我的代码使用。

但是,最近,我使用一项服务完成了这项工作,并根据他们未进行身份验证的原因构建结果。所以,像这样:

AuthenticateResult result = slAuthenticator.Authenticate(username, password)
if (result.authenticated) {
    user = result.user
} else {
    String message = result.failureReason; 

Sounds more like a specification doesn't it?

if (canAuthenticateSpec.isSatisfiedBy(username, password))

In the past, I have done this with a specification, so I can clearly and simply have that check around for my code to use.

But, recently, I did this with a service that does the work, and builds a result based on why they didn't authenticate. So, something, like this:

AuthenticateResult result = slAuthenticator.Authenticate(username, password)
if (result.authenticated) {
    user = result.user
} else {
    String message = result.failureReason; 
对你而言 2024-12-12 05:18:17

存储库是一种封装存储、检索和存储的机制
模拟对象集合的搜索行为。

我认为从存储库返回布尔值是可以的,但前提是该方法涉及此存储库中的所有对象。存储库通常公开类似集合的接口。例如,users.IsEmpty()users.IsNameUnique("jdoe") 似乎合乎逻辑,因为它们需要访问整个集合。

在您的场景中,仅按名称查找用户是可以分配给存储库的责任。检查密码是否有效似乎不太适合存储库。因此,第二种更详细的方法对我来说似乎更好。

或者,您可以在域中拥有专用的身份验证器接口,并在数据访问层中实现(类似于存储库的实现方式)。或者Authenticator可以成为内部使用存储库的域服务。身份验证器还可以区分“未找到用户”或“密码无效”,这可能具有不同的业务含义。

A Repository is a mechanism for encapsulating storage, retrieval, and
search behavior which emulates a collection of objects.

I think it is OK to return boolean from a repository but only if the method involves all the objects in this repository. Repository usually exposes collection-like interface. For example users.IsEmpty() or users.IsNameUnique("jdoe") seem logical because they need access to the whole collection.

In your scenario, only finding user by name is the responsibility that can be assigned to the repository. Checking whether password is valid or not does not seem like a good fit for repository. So the second, more verbose, approach seems better to me.

Alternatively you can have a dedicated Authenticator interface in your domain and implementation in data access layer (similarly to how repositories are implemented). Or Authenticator can become a domain service that uses repository internally. Authenticator can also tell between 'user not found' or 'password invalid' which would probably have different business meaning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文