Search code examples
language-agnosticdomain-driven-designrepositoryrepository-patternddd-repositories

DDD: can a repository return boolean values?


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)) {
{
    // ...

Solution

  • 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;