This is a language independent question but I write the sample codes in PHP.
We have a two classes :
- User class
- UserRepository class
UserRepository class deals with the interaction with DB and loading the required User in to a User object and return it.
Within the User class let's say there is an email attribute which is not loaded in the first place, and it would be lazy-loaded from Database whenever it's needed. But User class should not be aware of the UserRepository class, so we create a email proxy here. Which is aware of the UserRepository and wheveer the email is needed it would ask it from the UserRepository.
So here is the Code so far :
User class
class User
{
private $username;
private $email_proxy;
public function __construct($username,EmailProxy $email_proxy)
{
$this->username = $username;
$this->email_proxy = $email_proxy;
}
public function get_username()
{
return $this->username;
}
public function get_email()
{
return $this->email_proxy->get_email($this->get_username());
}
}
UserRepository class
class UserRepository
{
private $db;
public function __construct($db)
{
$this->db = $db;
}
/**
* @param string username
* @return string email , email address of the user
*/
public function get_email_by_username($username)
{
// interaction with DB, ...
return $user_email;
}
}
EmailProxy class
class EmailProxy
{
private $user_repository;
public function __construct(UserRepository $repository)
{
$this->user_repository = $repository;
}
public function get_email($username)
{
return $this->user_repository->get_email_by_username($username);
}
}
And here is the usage sample :
$user_repository = new UserRepository($db_instance);
$email_proxy = new EmailProxy();
$user = new User('my_username', $email_proxy);
So far so good. But here is the tricky part which I need your help about. By it's nature, a UserRepository should be responsible for fetching a User object from DataBase, constructing a User object and returning it. Like below :
class UserRepository
{
private $db;
public function __construct($db)
{
$this->db = $db;
}
public function get_email_by_username($username)
{
// interaction with DB, ...
return $user_email;
}
public function get_user_by_username($username)
{
// fetch the User from DB and ...
return new User($fetched_username) // missing the EmailProxy
}
}
So my question is how do pass the EmailProxy to the User object which is created by UserRepository ?
Do you inject the UserProxy instance to UserRepository so you could inject it to newly created User objects ?
Would you use a Dependency Injection Container for this?
Would you use a factory?
EDIT
EmailProxy is already aware of UserRepository, if we pass EmailProxy to UserRepository as well, it would be a circular dependency.
Any code/comments would be appreciated.
Thanks,