İş desen Bağımlılık Enjeksiyon ve Birim

0 Cevap php

I have a dilemma. I've used DI (read: factory) to provide core components for a homebrew ORM. The container provides database connections, DAO's,Mappers and their resultant Domain Objects on request.

İşte Mappers ve Domain Nesne sınıfları temel bir taslak var

class Mapper{
    public function __constructor($DAO){
        $this->DAO = $DAO;
        }

    public function load($id){
        if(isset(Monitor::members[$id]){
        return Monitor::members[$id];

        $values = $this->DAO->selectStmt($id);
        //field mapping process omitted for brevity
        $Object = new Object($values);
        return $Object;
        }
    }

 class User(){
     public function setName($string){
        $this->name = $string;
        //mark modified by means fair or foul
     }
 }

ORM ayrıca Çalışma desen ie Birimine dayalı bir sınıf (Monitör) içerir

class Monitor(){
    private static array modified;
    private static array dirty;
    public function markClean($class);
    public function markModified($class);
}

ORM sınıfı kendisi DI konteyner çıkarılan sadece koordinatlar kaynaklar. Yani, yeni bir User nesnesi örneğini:

$Container = new DI_Container;
$ORM = new ORM($Container);
$User = $ORM->load('user',1);
//at this point the container instantiates a mapper class
//and passes a database connection to it via the constructor
//the mapper then takes the second argument and loads the user with that id
$User->setName('Rumpelstiltskin');//at this point, User must mark itself as "modified"

Benim soru budur. Bir kullanıcı Alan Object sınıf değerlerini ayarlar noktada, ben Monitor sınıfında "kirli" olarak sınıf işaretlemek gerekir. Onu gördüğünüz gibi ben bir üç seçeneğiniz var

1: Pass an instance of the Monitor class to the Domain Object. I noticed this gets marked as recursive in FirePHP - i.e. $this->Monitor->markModified($this)

2: Instantiate the Monitor directly in the Domain Object - does this break DI? 3: Make the Monitor methods static, and call them from inside the Domain Object - this breaks DI too doesn't it?

Eylem için önerilen kurs ne olacağını (varolan bir ORM kullanmak dışında, ben eğlenmek için yapıyorum ...)

0 Cevap