Bir bağımlılık olarak enjekte önce bir nesne tam tam olmalıdır?

0 Cevap php

Bu soru bir uzantısıdır: http://stackoverflow.com/questions/3027082/understanding-how-to-inject-object-dependencies. Biraz farklı olduğu için, ben, umarım, cevap kolay, onları ayırmak için bunu yapmak istedim. Ayrıca, bu hepimizin aşina olacağını düşündüm gerçek bir sistem, sadece basitleştirilmiş bir örnek değildir. TIA. :

DB

ipler: thread_id, thread_name, vb

mesajlar: post_id, thread_id, post_name, post_contents, POST_DATE, post_user_id, vb

Overview

Temelde $ post_id yüklemek ve bunun hakkında bilmek istiyorum ve ben denetleyici sıska tutmaya çalışıyorum başka şeyler basamakla ve yüklemek için en sürdürülebilir şekilde bakıyorum. ANCAK:

  • Ben enjekte çok bağımlılıkları ile biten ediyorum
  • Ben başlatılmış ama boş nesneler geçiyorum
  • Ben etrafta geçirerek kaç tane parametre sınırlamak istiyorum
  • ( çok) Ben $ yazı enjekte edebilir, ancak bu sayfada ben bir iş parçacığı bakmıyorum, ben bir yazı bakıyorum
  • Ben birleştirmek olabilir / yeni bir nesne içine enjekte

Detail

Ben başka bir nesne enjekte ediyorsam, o tamamen ilk oluşturulduğu için en iyisidir? Ben bir sayfaya geçmek zorunda kaç tane parametre sınırlamak için çalışıyorum, ama ben bir daire ile sonuna kadar.

// 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'));

Ya, ben yeni bir nesne oluşturmak ve içine $ yazı ve $ iplik hem enjekte, ama sonra bağımlılıkları giderek artan sayıda nesne olabilir.

0 Cevap