نمط طبقة الخدمة في XOOPS
2.5.x ✅ 4.0.x ✅
نمط طبقة الخدمة يغلف منطق الأعمال في فئات خدمة مخصصة، مما يوفر فصلاً واضحاً بين المتحكمات وطبقات الوصول للبيانات. يعزز هذا النمط إعادة استخدام الكود وسهولة الاختبار والقابلية للصيانة.
مفهوم طبقة الخدمة
Section titled “مفهوم طبقة الخدمة”طبقة الخدمة:
- تحتوي على منطق الأعمال في المجال
- تحسق بين عدة مستودعات
- تتعامل مع العمليات المعقدة
- تدير المعاملات
- تجري التحقق والتفويض
- توفر عمليات عالية المستوى للمتحكمات
الفوائد
Section titled “الفوائد”- منطق أعمال قابل لإعادة الاستخدام في جميع المتحكمات
- سهولة الاختبار بشكل منفصل
- تنفيذ مركزي لقواعد الأعمال
- فصل واضح للمخاوف
- كود متحكم مبسط
حقن الاعتماديات
Section titled “حقن الاعتماديات”<?php// خدمة مع اعتماديات مُحقونةclass UserService{ private $userRepository; private $emailService;
public function __construct( UserRepositoryInterface $userRepository, EmailServiceInterface $emailService ) { $this->userRepository = $userRepository; $this->emailService = $emailService; }
public function registerUser($username, $email, $password) { // التحقق $this->validate($username, $email, $password);
// إنشاء مستخدم $user = new User(); $user->setUsername($username); $user->setEmail($email); $user->setPassword($password);
// حفظ $userId = $this->userRepository->save($user);
// إرسال بريد ترحيب $this->emailService->sendWelcome($email, $username);
return $userId; }
private function validate($username, $email, $password) { $errors = [];
if (strlen($username) < 3) { $errors['username'] = 'Username too short'; }
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors['email'] = 'Invalid email'; }
if (strlen($password) < 6) { $errors['password'] = 'Password too short'; }
if (!empty($errors)) { throw new ValidationException('Invalid input', $errors); } }}?>حاوية الخدمة
Section titled “حاوية الخدمة”<?phpclass ServiceContainer{ private $services = [];
public function __construct($db) { // تسجيل المستودعات $this->services['userRepository'] = new UserRepository($db);
// تسجيل الخدمات $this->services['userService'] = new UserService( $this->services['userRepository'] ); }
public function get($name) { if (!isset($this->services[$name])) { throw new \InvalidArgumentException("Service not found: $name"); } return $this->services[$name]; }}?>الاستخدام في المتحكمات
Section titled “الاستخدام في المتحكمات”<?phpclass UserController{ private $userService;
public function __construct(ServiceContainer $container) { $this->userService = $container->get('userService'); }
public function registerAction() { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { return []; }
try { $userId = $this->userService->registerUser( $_POST['username'], $_POST['email'], $_POST['password'] );
return [ 'success' => true, 'userId' => $userId, ]; } catch (ValidationException $e) { return [ 'success' => false, 'errors' => $e->getErrors(), ]; } }}?>أفضل الممارسات
Section titled “أفضل الممارسات”- كل خدمة تتعامل مع مخاوف مجال واحدة
- الخدمات تعتمد على الواجهات وليس على التنفيذات
- استخدم حقن المنشئ للاعتماديات
- يجب أن تكون الخدمات قابلة للاختبار بشكل منفصل
- ارمِ استثناءات خاصة بالمجال
- يجب ألا تعتمد الخدمات على تفاصيل طلب HTTP
- احفظ الخدمات مركزة ومتماسكة
الوثائق ذات الصلة
Section titled “الوثائق ذات الصلة”انظر أيضاً:
- MVC-Pattern للتكامل مع المتحكم
- Repository-Pattern للوصول للبيانات
- DTO-Pattern لكائنات نقل البيانات
- Testing لاختبار الخدمة
الوسوم: #service-layer #business-logic #dependency-injection #design-patterns