代码之家  ›  专栏  ›  技术社区  ›  Amit Shah

laravel:orm独立存储库模式

  •  0
  • Amit Shah  · 技术社区  · 6 年前

    我偶然发现了关于在laravel中实现存储库模式的不同文章,所有这些都让我感到困惑。他们每个人都在努力 “保持独立” 但没有人真正展示示例代码。

    我在这里使用我的存储库示例结构,我认为它在某种程度上不是独立于orm的。但我需要它 真实存储库模式 我可以从中更改orm的解决方案 Eloquent 模仿其他类似的东西 教条 . 并保持 业务逻辑 分开一点,这样我就不需要改变了。目前,我的业务逻辑在存储库中(我认为不推荐)。

    基本问题:

    1. 我的存储库使用了雄辩的方法名,如果我更改orm,它将不在其中。 $this->model = $shops;
    2. 在控制器和刀片模板中,我们正在玩的是 Collections 属于 Eloquent Model . 如果我们更改orm,我们应该如何处理它们?
    3. 如果不在存储库中,则将create/delete方法放在何处。

    请不要只是使用 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'));
        }
    
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Abdullah Al Shakib    6 年前

    首先创建基本存储库。然后扩展这个存储库。试试这边。可能对你有帮助

    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();
        }
    
    }