Should an object be fully complete before injected as a dependency?
Posted
by Hans
on Stack Overflow
See other posts from Stack Overflow
or by Hans
Published on 2010-06-12T13:57:11Z
Indexed on
2010/06/12
14:02 UTC
Read the original article
Hit count: 183
php
|dependency-injection
This is an extension of this question: http://stackoverflow.com/questions/3027082/understanding-how-to-inject-object-dependencies. Since it is a bit different, I wanted to separate them, to make it, hopefully, easier to answer. Also, this is not a real system, just a simplified example that I thought we'd all be familiar with. TIA. :
DB
threads: thread_id, thread_name, etc
posts: post_id, thread_id, post_name, post_contents, post_date, post_user_id, etc
Overview
Basically I'm looking at the most maintainable way to load $post_id and have it cascade and load the other things I want to know about and I'm trying to keep the controller skinny. BUT:
- I'm ending up with too many dependencies to inject
- I'm passing in initialized but empty objects
- I want to limit how many parameters I am passing around
- I could inject $post(->many) into $thread(one<-), but on that page I'm not looking at a thread, I'm looking at a post
- I could combine/inject them into a new object
Detail
If I am injecting an object into another, is it best to have it fully created first? I'm trying to limit how many parameters I have to pass in to a page, but I end up with a circle.
// 1, empty object injected via constructor
$thread = new Thread;
$post = new Post($thread); // $thread is just an empty object
$post->load($post_id); // I could now do something like $post->get('thread_id') to get everything I want in $post
// 2, complete object injected via constructor
$thread = new Thread;
$thread->load($thread_id); // this page would have to have passed in a $thread_id, too
$post = new Post($thread); // thread is a complete object, with the data I need, like thread name
$post->load($post_id);
// 3, inject $post into $thread, but this makes less sense to me, since I'm looking at a post page, not a thread page
$post = new Post();
$post->load($post_id);
$thread = new Thread($post);
$thread->load(); // would load based on the $post->get('post_id') and combine. Now I have all the data I want, but it's non-intuitive to be heirarchially Thread->Post instead of Post-with-thread-info
// Or, I could inject $post into $thread, but if I'm on a post page,
// having an object with a top level of Thread instead of
// Post-which-contains-thread-info, makes less sense to me.
// to go with example 1
class post
{
public function __construct(&$thread)
{
$this->thread=$thread;
}
public function load($id)
{
// ... here I would load all the post data based on $id
// now include the thread data
$this->thread->load($this->get('thread_id'));
return $this;
}
}
// I don't want to do
$thread = new Thread;
$post = new Post;
$post->load($post_id);
$thread->load($post->get('post_id'));
Or, I could create a new object and inject both $post and $thread into it, but then I have object with an increasing number of dependencies.
© Stack Overflow or respective owner