我有我创建模型的存储库模式。我想每次创建模型时都调用事件,我应该从控制器还是从存储库中调用?如果我在存储库模式中这样做,我会打破单一责任原则吗?这是我的代码示例
class FriendshipRepository implements IFriendshipStorageRepository
{
public function create(User $user, User $follower): Friendship
{
$friendship = Friendship::create([
'user_id' => $user->id,
'follower_id' => $follower->id
]);
event(new FriendshipCreatedEvent($friendship));
return $friendship;
}
public function destroy(User $user, User $userToUnfollow)
{
$friendship = $user->getFriendship($userToUnfollow);
$friendship->delete();
event(new FriendshipDeletedEvent($friendship));
}
}
或者我应该在控制器和存储库之间多做一层?新层将使用存储库和调用事件,但控制器只会调用该新层。
白衣染霜花
暮色呼如