Title: Service Layer in PHP
1Service Layer in PHP
2Service Layer in PHP
- Service Layer is a list of service classes that
are used to handle application logic. Service
classes are distinguished from used from
different front ends. - At every level of abstraction, Source code is
used to represent the data entities and their
behavior. In object-oriented programming, there
are different types of logic that can be
partitioned into
3- Application logic It is the scope of a service
layer and it is not domain-specific. For example,
converting objects into JSON or XML is a function
of application logic. It can even be used with
application-specific classes. The task of showing
data in a particular format is unobserved to the
domain. As it is not domain-specific. - Business logic It is encapsulated in a domain
model. And it is specific to the particular
domain the application works in. In an enterprise
application, all added value depends on business
logic. - Presentation logic Presentation logic is
considered as the subset of application logic.
Which handled the end-user view of the
application.
4- All those different aspects of an application are
organized into different layers. Where each layer
resides on the top of the previous layer. In
older approaches, there is an infrastructure
layer on which the business logic depends and
which can deal with persistent issues. - A Service Layer is useful when there are multiple
front-ends to a common domain model. These
interfaces or APIs delegate the application logic
to the service layer. Which may encapsulate each
other. - Responsibility of Service Layer- It includes the
responsibility of CRUD function(s) over the
object of a specific domain model. Some of the
responsibilities are already included while
others are only specified as interfaces.
5Example code
- It includes two service classes one as the
implementation of the interface of the domain
model and another is only employed at a higher
level - lt?php
- require_once 'domain.php'
- /
- Adapter for an interface defined in the lower
layer - /
6- class EmailRepositoryClass implements
EmailRepository -
- public function getAllEmail(address)
-
- // fake implementation
- mail new Email()
- mail-gtsetSender(sunil_at_example.com')
- mail-gtsetRecipient(sunilseth9i_at_example.com')
- mail-gtsetSubject('testing')
- mail-gtsetText('it is just testing the class')
- return array(
- mail
7- )
-
-
- /
- Service that belongs only to the very Service
Layer. - It may have an interface, but it will be kept
in this layer. - /
- class EmailTransferService
8- /
- Transforming an object to XML is a common task
for a Service Layer. - However, dependencies towards the Domain Model
are well-accepted - but they don't mean that this layer's classes
should be - included in the lower layer.
- Keeping the XML transformation in one place
aids different front-end - /
- public function toXml(Email mail)
9- return "ltmailgt\n"
- . " ltsendergt" . mail-gtgetSender() .
"lt/sendergt\n" - . " ltrecipientgt" . mail-gtgetRecipient() .
"lt/recipientgt\n" - . " ltsubjectgt" . this-gt_encode(mail-gtgetSubject(
)) . "lt/subjectgt\n" - . " lttextgt" . this-gt_encode(mail-gtgetText()) .
"lt/textgt\n" - . "lt/mailgt\n"
10- protected function _encode(text)
-
- return htmlentities(text)
-
-
- // client code
- repository new EmailRepositoryClass()
- transferService new EmailTransferService()
- foreach (repository-gtgetAllEmail('bob_at_example.com
') as mail) - echo transferService-gttoXml(mail)
11Online Testing Certification Exam StudySection
offers Testing Online Certifications at
Foundation Advanced Expert levels apart from
many other free online certification exams.
12About StudySection
Welcome to StudySection - the most loved online
platform for eCertification in several subjects
including but not limited to Software
Development, Quality Assurance, Business
Administration, Project Management, English,
Aptitude and more. From more than 70 countries
students are StudySection Certified. If you are
not yet StudySection certified it's not late. You
can start right now. Being StudySection
Certified helps you take your education level few
notches up and have an edge over other candidates
when you need it the most. Globally, our students
are employed in different organizations and are
utilizing the benefit of being certified with us.