Rationale behind Dependency Injection Container libraries

Well in the above code, you are expecting that $blog->getAuthor() will work properly, but how? If BlogRepository does not contain a dependency of UserRepository(Author should be an instance of domain model User), then how is it supposed to create a User domain model just by calling $blogRepository->find($blogID)? Do you mean, the BlogRepository should be able to create a User domain model on its own? Like, writing a JOIN query and manually create the User object for $blog->author property? This is a part of the code in my PostRepository that generates a complete Post object, the loadModel($object) is called inside each findByXYZ method for repository to load the model:

// $object is a stdclass object generated from PDO::fetchObject(), which contains only data and is used only during model creation.
protected function loadModel($object){
    $post = new Post;
    $post->setID($object->id);
    $post->setUser($this->userRepository->find($object->user))
    $post->setTitle($object->title);
    $post->setContent($object->content);
    $post->setDatePosted(new DateTime($object->dateposted));
    $post->setStatus($object->status);
    return $post;
}

Actually I looked up an example with foreign key mapping in Martin Fowler’s book ‘Patterns of Enterprise Application Architecture’. This is how it is done in his example:

protected DomainObject doLoad(long id, ResultSet rs) throws SQLException{
    string title = rs.getString(2);
    long artistID = rs.getLong(3);
    Artist artist = MapperRegistry.artist().find(artistID);
    Album result = new Album(id, title, artist);
}

As you can see, my repository actually mirrors the mapper in Martin Fowler’s book. The differences are:

  1. ResultSet in Fowler’s book is specific to Java, while in my book I get a stdclass object to hold temporary data. But anyway, they both are just temporary data structure to transfer data from DB to model.
  2. Fowler’s Album class is simple enough so he passes the three fields to Album constructor, while I use setters since there are a lot more fields(dont want to have a constructor with 5+ parameters).
  3. Fowler uses Registry to fetch ArtistMapper for AlbumMapper to use, while I use dependency injection to push an instance of UserRepository to PostRepository, thus removing the need for Registry.

As you see, Fowler’s approach is essentially the same with mine, and my repository works even better since I am using dependency injection rather than relying on a static registry. I understand that by the time Fowler wrote the book(2002-2003), the concept of DI aint widely adopted so singletons and registries were common at that time. If Fowler rewrites the book today, he would definitely use dependency injection. Thus passing ArtistMapper to AlbumMapper as a dependency, like how I am passing UserRepository to PostRepository as dependency.

So can you illustrate by an example of how you would approach this problem, if PostRepository is not dependent on UserRepository?