php - Provide dependencies to give behaviour to Entities -
this kind of general question, i'll try describe use case hoping it'll clear enough
i have created set of entities model domain. every entity has own attributes describing properties.
suppose 1 user entity (i use php example)
class user { private $email; private $password; }
now give entities more advanced behaviour getting , setting attributes.
for example suppose i'd create such method
public function authenticate($email, $password)
to this, methods i'd write need dependencies. in example authenticationservice
.
now, question is, 1 best practice pass these dependencies method?
some options be:
1) not pass dependencies. put entity behaviour in other service.
2) pass dependencies in constructor on entity. if question is: correct entity depends on external service?
class user { private $authenticationservice public __construct($authenticationservice) { $this->authenticationservice = $authenticationservice; } }
3) pass dependencies arguments of method call
public function authenticate($email, $password, $authenticationservice)
which best solution? there options other 1 listed above?
well, old discussion martin fowler calls anemic domain model anti-pattern.
although no means think more qualified talk great martin fowler, pretty agree article anemic domain models being more respectful solid design principles.
so, ihmo, if user
entity, should popo (plain old php object), or maybe have logic inherent itself, example:
class user { //... public function isadmin() { return $this->role == self::role_admin; } //... }
when make authenticaionservice
object part of entity through composition, probabaly breaking single responsibility principle (srp) , surely breaking separation of concerns principle (soc).
also, can think way:
an user
entity because exists itself, not require else user. while authenticationservice
service (duh!) check previleges of user. so, depends on user useful.
therefore, user
object should parameter of authenticationservice
object , not other way around.
Comments
Post a Comment