ERPClimb logoERPClimb
Modularization
ABAP Developmentadvanced 9 min read

Modularization Part 9: Reusable Service Class Design

Preview · a free account unlocks the full lesson

Key takeaway

Design a reusable global service class for a business object: clear responsibility, small public interface, separation of data access and logic, exceptions, buffering, and how function modules, reports and BAdIs consume it.

In this lessonShow sections

Visual overview

Modularization Part 9: Reusable Service Class Design — learning sheetIn one line: Design a reusable global service class for a business object: clear responsibility, small public interface, separation of data access and logic, exceptions, buffering, and how function modules, reports and BAdIs consume it.ERPClimb.com / ABAP / LESSON 9/10 / LEARNING SHEETModularization Part 9: Reusable Service Class Design1 · IN ONE LINEDesign a reusable global service class for a business object: clear responsibility, smallpublic interface, separation of data access and logic, exceptions, buffering, and howfunction modules, reports and BAdIs consume it.2 · KEY IDEAS EXPLAINEDResponsibilityZCL_FLIGHT_BOOKING_SERVICE: check availability,book a seat, cancel a booking, get bookings of acustomer. Not: ALV display, file upload, mail…Layers Inside The ServicePublic methods: business operations(check_availability, book_seat).Data access: a separate class/interfaceZIF_FLIGHT_DAO with read/write methods (SELECT,updates) — replaceable in tests (Part 10).Interface DesignBusiness names: book_seat( is_request ) RETURNINGrv_booking_id RAISING zcx_flight_booking.Input structures for many fields; output viaRETURNING or EXPORTING structures.Instance CreationFactory method (get_instance / create) orconstructor injection for the DAO.Singleton only if the service holds a sharedbuffer and is stateless otherwise.3 · DO · AVOIDBest practice!Common mistakeOne business responsibility per service.Small public interface with business names andexceptions.Separate data access behind an interface.✕Services that also do UI (ALV, messages).✕COMMIT WORK inside reusable services.✕Huge public interfaces exposing internalhelpers.4 · IN A REAL PROJECTBooking logic existed in an online report, an RFC module for a web portal and a nightly job — threeimplementations with different checks. A portal booking bypassed the "no overbooking" rule that onlythe report had. The team created ZCL_FLIGHT_BOOKING_SERVICE with the rules in one place, a DAO interface for…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Parts 6 through 8 built up classes and interfaces individually. This part combines them into a design you can reuse consistently across an entire project: the service class.

A service class owns one business area (e.g. flight bookings) and offers a small, stable public interface. ZCL_FLIGHT_BOOKING_SERVICE, for instance, would own checking availability, booking a seat, canceling a booking, and retrieving a customer's bookings — but it would not own ALV display, file upload, or sending mail, since those belong to other classes entirely.

Inside the service, it helps to separate data access (DAO/repository) from business logic — easier to test and change. Public methods expose the actual business operations, such as check_availability and book_seat. Data access lives in a separate class or interface, say ZIF_FLIGHT_DAO, with its own read and write methods covering SELECTs and updates — kept separate specifically so it can be swapped out in tests, a topic covered in Part 10. Validation logic can live either in private methods or in a dedicated validator class.

Interface design deserves its own attention: methods should carry business names, like book_seat( is_request ) RETURNING rv_booking_id RAISING zcx_flight_booking. Where there are many input fields, an input structure is cleaner than a long parameter list, and output should go through RETURNING or EXPORTING structures. Exceptions should be declared with meaningful texts, backed by a message class — the service raises them, and it's up to the caller to decide how to actually display them to a user.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.