依赖注入和工作单元模式 [英] Dependency Injection and Unit of Work pattern

查看:73
本文介绍了依赖注入和工作单元模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到两难。我使用DI(阅读:工厂)来提供自制ORM的核心组件。
该容器可应要求提供数据库连接,DAO,映射器及其结果域对象。

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.

以下是Mappers和Domain Object类的基本概述

Here's a basic outline of the Mappers and Domain Object classes

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还包含基于工作单位模式的类(监视器),即

The ORM also contains a class (Monitor) based on the Unit of Work pattern i.e.

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

ORM类本身只是协调从DI容器中提取的资源所以,实例化一个新的User对象:

The ORM class itself simply co-ordinates resources extracted from the DI container. So, to instantiate a new User object:

$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"

我的问题是这个。当用户在域对象类上设置值时,我需要在Monitor类中将该类标记为脏。我可以看到三个选项之一

My question is this. At the point when a user sets values on a Domain Object class, I need to mark the class as "dirty" in the Monitor class. I have one of three options as I can see it

1:将Monitor类的实例传递给Domain Object。我注意到这在FirePHP中被标记为递归-即
$ this-> Monitor-> markModified($ this)

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:直接在域对象-这会破坏DI吗?
3:将Monitor方法设为静态,然后从Domain Object内调用它们-这样也会破坏DI吗?

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?

您会推荐什么的操作过程(除了使用现有的ORM之外,我还是在做这件事……)

What would be your recommended course of action (other than use an existing ORM, I'm doing this for fun...)

推荐答案

好的,我看到。怎么办?

Okay, I see. What about this:

 $user = new User;
 $monitoredUser = new Monitor( $user );

 //at update in class Monitor:
 Monitor::markDirty( $this );

现在您将Monitor用作装饰器;它在用户对象或任何其他处理传入请求的对象周围添加外壳。如果对象被更新,则监视器将其标记为脏,但是由于监视器为此使用静态方法,因此它保持在当前类范围内,从而避免了递归。

Now you use the Monitor as a decorator; it adds a shell around the user object or any other object that handles incoming requests. If an object gets updated it will be marked dirty by the monitor, however because the Monitor uses a static method for this it remains within the current class scope thus avoiding recursion.

这篇关于依赖注入和工作单元模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆