将Eloquent模型与业务逻辑分离

时间:2014-12-16 00:50:56

标签: php laravel orm solid-principles single-responsibility-principle

我想编写离散框架不可知模型。

我为所有这些模型编写了接口。

问题在于实现这些接口时,例如Eloquent I将我的所有业务逻辑链接到ORM。

例如,我想在addVariation模型上使用方法Product

界面

interface ProductInterface
{   
    /**
     * Adds a variation
     *
     * @param VariationInterface $variation
     */
    public function addVariation(VariationInterface $variation);

    // ...
}

具体化

class Product extends Model 
{
    /**
     *  @param Collection | VariationInterface
     */
    protected $variations;

    public function addVariation(VarientInterface $varient)
    {
        if( ! $this->hasVariation($varient) ) 
        {
            $this->variations->add($variations);
        }

        return $this;
    }
}

我遇到的问题是我的所有业务逻辑都存在于我的模型的特定Eloquent ORM实现中。

我怎么可能把它分开呢?我能看到的唯一真正的依赖是我需要某种类型的集合类?或者我可以只使用普通的旧数组?

我只是不想将我的所有逻辑链接到特定的ORM 我想保持框架不可知。

1 个答案:

答案 0 :(得分:2)

只需从Eloquent ORM中删除所有逻辑。

您只需要一个ORM就可以更轻松地从数据库中保存和检索数据。您应该使用普通的旧php对象编写所有业务逻辑。然后,您可以创建一些所有业务逻辑模型使用的通用PersistenceGateway接口,例如。

interface PersistenceGatway {

   public function saveGatewayData($id, array $data);

   public function retrieveGatewayData($id)
}

您的解耦业务逻辑将此接口用于saveretrieve数据。然后,您需要做的就是使用您选择的ORM实现接口(或者您可能还需要创建一些适配器类来帮助您)。您现在可以插入任何您喜欢的ORM,只要它实现PersistenceGateway接口。

看看Uncle Bobs Clean Architecture。像Laravel这样的Web框架应该是您的应用程序/业务逻辑的插件,而不是相反。

编辑:非常基本的例子。

class SomeBusinessLogic {

   // Note: Class depends on the PersistenceGateway. Any class
   // that implements this interface can be passed in. This is
   // essentially how you decouple logic from ORMS. You can now 
   // implement this interface with any ORM you like and pass it in
   public __construct(PersistenceGateway $gateway){
      $this->gateway = $gateway;
   } 

   public function someLogicMethod($id){
      // do something and save state to the gateway
      $this->gateway->saveGatewayData($id, ['some_state'=>'value']);
   }

   public function someDataReturnMethod($id){
      return $this->gateway->retrieveGatewayData($id);
   }

}

// Im extending from Eloquent here, but you can do a similar thing
// with any ORM.
class SomeEloquentModel extends Eloquent implements PersistenceGateway {

   public function saveGatewayData($id, array $data){
       $model = $this->find($id);
       // call eloquent save method
       $model->save($data);
   }

   public function retrieveGatewayData($id){
       // retrieve the data. Important to return
       // an array NOT an eloquent model, otherwise
       // we are still coupled. I think toArray() is
       // the correct method to call on eloquent model
       return $this->find($id)->toArray();
   }
}

class SomeController {

    class someControllerMethod {
       // Your controller decides on the implementation of PersistenGateway
       // to use. You could also use an IoC container which would be a slightly
       // cleaner solution
       $logic = new SomeBusinessLogic(new SomeEloquentModel());
       $logic->someLogicMethod(Input::get('id'));
       return $logic->someDataReturnMethod(Input::get('id'));
    }

}