vendredi 19 juin 2015

Provide dependencies to give behaviour to Entities

This is kind of a general question, so I'll try to describe my use case hoping that it'll be clear enough

I have created a set of entities to model my domain. Every entity has its own attributes describing its properties.

Suppose one is the User entity (I use PHP for the example)

class User
{
    private $email;
    private $password;
}

Now I would like to give my entities some more advanced behaviour than getting and setting its attributes.

For example suppose I'd like to create such a method

public function authenticate($email, $password) 

To do this, the methods I'd like to write need some dependencies. In the example this could be an AuthenticationService.

Now, my question is, which one is the best practice to pass these dependencies to my method?

Some options could be:

1) Do not pass dependencies. Put all the entity behaviour in some other service.

2) Pass the dependencies in the constructor on my Entity. If so my question is: is it correct that an Entity depends on an external service?

class User
{
    private $authenticationService

    public __construct($authenticationService)
    {
        $this->authenticationService = $authenticationService;
    }
}

3) Pass the dependencies as arguments of my method call

public function authenticate($email, $password, $authenticationService)

Which is the best solution? Are there any options other than the one that I listed above?

Aucun commentaire:

Enregistrer un commentaire