我偶然发现了有关在Laravel中实现存储库模式的不同文章,所有这些使我感到困惑。他们每个人都在强调“保持ORM独立性” ,但实际上没有人显示示例代码。
我在这里是我的存储库样本结构,我认为它在某种程度上不是ORM独立的。但是我需要使用 REAL REPOSITORY PATTERN 解决方案,在该解决方案中,我可以将ORM从Eloquent
模型更改为诸如 Doctrine 的其他名称。并以某种方式保持业务逻辑分开,因此我无需进行更改。目前,我的业务逻辑位于存储库中(我认为不建议这样做)。
基本问题:
$this->model = $shops;
Collections
中的Eloquent Model
。如果更改ORM,应该如何处理?请不要只使用
Domain Object
一词,因为我已经厌倦了 没有编码示例就可以理解它。这将是高度 如果您尝试使用实际代码解释[Domain object],则不胜感激 修改此示例。 [如何在控制器中返回并使用它 在刀片服务器中]
接口:
<?php
namespace Modules\ShopOwner\Entities\Repository\Contract;
interface ShopsRepository {
public function getall();
public function create($data);
public function update($id, $data);
public function delete($id);
}
口才:
<?php
namespace Modules\ShopOwner\Entities\Repository\Eloquent;
use Illuminate\Database\Eloquent\Model;
use Modules\ShopOwner\Entities\Repository\Contract\ShopsRepository;
use Modules\ShopOwner\Entities\Shops;
class ShopsEloquent implements ShopsRepository
{
protected $model;
public function __construct(Shops $shops)
{
$this->model = $shops;
}
public function getall()
{
return $this->model::with('shopadmin')->get();
}
public function create($data)
{
$this->model::create($data);
}
public function update($id, $data)
{
$this->model::findOrFail($id)->update($data);
}
public function delete($id)
{
$this->model::findOrFail($id)->delete();
}
}
控制器:
<?php
namespace Modules\ShopOwner\Http\Controllers;
use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Modules\ShopOwner\Entities\Repository\Contract\ShopsRepository;
use Modules\ShopOwner\Entities\Shops;
use Validator;
class ShopsController extends Controller
{
protected $shops;
public function __construct(ShopsRepository $shops)
{
$this->shops = $shops;
}
public function index()
{
$shops = $this->shops->getall();
return view('shopowner::shops.list', compact('shops'));
}
public function store(Request $request)
{
$data = $request->all();
$this->shops->create($data);
return redirect()->route('SO.shops.index')->with('success', __('shopowner::shops.create_success'));
}
public function update(Request $request, $id)
{
$data = $request->all();
$this->shops->update($id, $data);
return redirect()->route('SO.shops.index')->with('success', __('shopowner::shops.update_success'));
}
public function delete($id)
{
$this->shops->delete($id);
return redirect()->route('SO.shops.index')->with('success', __('shopowner::shops.remove_success'));
}
}
答案 0 :(得分:0)
首先创建一个基础存储库。然后扩展此存储库。尝试这种方式。可能会对您有帮助
interface BaseRepository{
public function getall();
public function create($data);
public function update($id, $data);
public function delete($id);
}
class BaseEloquent implements BaseRepository{
protected $model;
public function getall(){
return $this->model->get();
}
....
....
}
interface ShopsRepository extends BaseRepository{
}
class ShopsEloquent extends BaseEloquent implements ShopsRepository{
public function __construct(Shops $shops){
$this->model = $shops;
}
public function getall(){
return $this->model::with('shopadmin')->get();
}
}