Where should I put the domain related constants

domain-driven-design

For example if I have a constant, like generated password size, then where should I put that constant if the random password generator is implemented in the infrastructure? I guess this should be a domain service, but I guess I cannot put the constant in the service interface. Or can I?

I ended up with this:

namespace Example\domain;

interface iRandomPasswordService {

    const RANDOM_PASSWORD_SIZE = 6;

    public function getPassword();

}

namespace Example\infrastructure;

use Example\domain\iRandomPasswordService;
use Example\domain\Password;
use JUserHelper;

class RandomPasswordService implements iRandomPasswordService {

    public function getPassword(){
        $randomPassword = JUserHelper::genRandomPassword(self::RANDOM_PASSWORD_SIZE);
        return new Password($randomPassword);
    }

}

I hate PHP and Joomla too, don't mention it. I am waiting for an approval or a better idea. 🙂

Best Answer

I think there are two acceptable places storing the length for generated passwords could occur at, which one is better depends on how much you control of password generation/authentication service and your actual app.

  1. Password length is sealed in the generation app itself because its really a concern for that service to manage that all password it creates are long enough to be secure.
  2. Desired password length is passed to the service by the app, and the app stores in in a constants/config file with other authentication/security related values. I wouldn't store this value on the interface because if it changes then you have to update the interface and everywhere that uses the interface.

I would prefer option 1 unless I couldn't modify the generation service itself because it keeps all user authentication concerns within the service you chose to manage them.