【发布时间】:2017-12-07 21:37:17
【问题描述】:
我最近遇到了 SOLID 设计原则,并一直在寻找将依赖注入合并到我的工作中的方法,以便我可以编写更好的测试并模拟任何外部调用。我在 PHP Laravel (Lumen) 中尝试了一些依赖注入,但下面的内容似乎不对。
创建依赖项的代码是在其中一个控制器的公共方法中创建的。这似乎不是放置它的合适位置,因为我现在有很多非常丑陋的代码在控制器内部实例化对象,而不仅仅是接收请求并打包响应。是否有更适合进行实例化的地方?也适用于某些物品,例如$paying_user object。我正在创建一个空的 Eloquent 模型,使用一种方法来检索集合中的第一项并将其与返回的对象交换以进行注入。这也感觉不对。谁能指出构建以下内容的最佳和正确方法应该是什么?
public function cancel_subscription(Request $request)
{
$result = false;
// Create dependencies
$platform_id = Sanitiser::clean_integer($request->input('platform_id'));
$paying_user = new PayingUserModel();
$paying_user = $paying_user->where('platform_id','=',$platform_id)->first();
$current_subscription_model = $paying_user->non_cancelled_subscriptions->first();
$refund_model = new RefundModel();
$stripe = new Stripe();
$stripe_refund = new Refund();
$charge = new ChargeModel();
$stripe::setAPIkey(env('STRIPE_SK'));
$stripe_subscription = new Subscription();
// Create services and inject
$this->cancellation_service = new CancellationService($paying_user,$current_subscription_model,$stripe_subscription);
$this->refund_service = new RefundService($paying_user,$current_subscription_model,$refund_model,$stripe_refund,$charge);
// Run services
$result = $this->cancellation_service->cancel_subscription();
if($this->cancellation_service->get_refund_required()==true){
$result = $this->refund_service->handle_refund();
}
}
【问题讨论】:
-
"它下面的 $paying_user 对象在被注入之前与实际的付费用户对象交换" ...什么?
-
$paying_user = new PayingUserModel(); $paying_user = $paying_user->where('platform_id','=',$platform_id)->first() //试图说我正在创建一个空的 Eloquent 模型,用它来检索集合的第一项(获取实际数据)用于我想注入其他类的模型。
-
“...在被注入之前”部分...当您自己调用方法或实例化对象时,不会发生“注入”
-
你在Controller的一个方法里做的太多了,首先你应该把关注点分开不同类型的对象/不同的地方。
-
如果我拆分方法并将退款活动移动到一个新方法,控制器是否仍然是实例化这些空对象并注入其他对象的最合适位置?
标签: php laravel dependency-injection