我想编写与框架无关的离散模型。
我为所有这些模型编写了接口。
问题在于在实现这些接口时,例如使用 Eloquent 我将我的所有业务逻辑链接到 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中,我想保持框架不可知。
只需从雄辩的ORM中删除所有逻辑即可。
您只需要一个ORM即可更轻松地从数据库中保存和检索数据。你应该用普通的旧 php 对象编写所有业务逻辑。然后,您可以创建一些所有业务逻辑模型使用的通用PersistenceGateway
接口,例如
interface PersistenceGatway {
public function saveGatewayData($id, array $data);
public function retrieveGatewayData($id)
}
分离的业务逻辑使用此接口来save
和retrieve
数据。然后你需要做的就是用你选择的ORM实现接口(或者你可能还需要创建一些适配器类来帮助你)。您现在可以插入任何您喜欢的 ORM,只要它实现了PersistenceGateway
接口。
看看鲍勃叔叔干净的建筑。像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'));
}
}