Service Layer
When use :
1.
Exchange With another system
2.
There are multiple clients(PC , phone , Pad)
3.
Boundary from client to server , Decide what client can do, control client behaviour
4.
Encapsulate business logic and expose API for client
5.
For SOA(Service oriented applicant)
Service Principles
1.
Boundary is explicit
Each Service API should be clean and simple
2.
Autonomous
Each API call should be independent , and atomic operation
3.
Shares schema and contract, not class
Should only expose contract , not implementation
Service Pattern
1.
Request/Response
Customer[] RetrieveCustomers(string country);
Customer[] RetrieveCustomers (string country, string postalCode);
Customer[] RetrieveCustomers (string country, string postalCode, string street);
public class CustomerSearchRequest
{
public string Country { get; set; }
public string PostalCode { get; set; }
public string Street { get; set; }
}
As We can see , instead of changeable parameters , we can use a encapsulate Request and Response class to exchange data between client and server
2.
Reservation
As We can see , this pattern is used when we need to finish a transaction by many steps, we can use an id to record the state of the steps for each transaction .such as we need to:
1.
order something online (server return an
order no.)
2.
purchase (sending
order no. to server, server return
purchase no.)
3.
check product (using
purchase no. to check status)
3.
Idempotent Messaging Pattern
Meaning each time request from client , if the request is same ,should get the same result from server .
As we can see, in this pattern ,server will cache the results for client ,if server have done with the same request ,then just retrieve the result in cache and return client .
Others
1.
If needed , we can add GUID to error log then return client , so if client want to check, then can easily locate the error in log file .
2.
If needed , can add a proxy layer wrapping the service APIs , use facade pattern giving client a simpler and clean API.