Analysis of third-party library integration methods

Source: Internet
Author: User

Some time ago, I integrated Sina Weibo SDK for iOS and implemented some functions. Here I will summarize the content I reflected later. You are welcome to try it out.

Assume that the requirement is as follows: if you have already logged on to Weibo, search for all the friends of a person and find out the persons whose age, gender, and other attributes comply with certain rules, search for all Weibo posts of these people and organize and display these Weibo posts according to certain conditions.

First, it is explained that the method of searching for friends and Weibo is to request data from Sina API through network requests, while login can be simply implemented through SDK.

From the above descriptions, we can see two major use cases: Login and a series of operations described later.

First of all, there are many login methods, such as Weibo, Facebook, and so on, aside from the application requirements. For an application, login is just an upper-layer use case, therefore, it is necessary for us to provide an operation (also called an event or service) for the login action, determine the login method internally, and call the specific login logic downward, in this case, the policy mode or factory mode is used to distinguish different situations.

Furthermore, any program may log on to Alibaba Cloud. Therefore, this login service must be a common module unrelated to the specific business logic, it should be independent from the entire system to ensure reuse. The subsequent query operations are related to specific applications. Because of different requirements, it cannot be reused in another application. Therefore, it is only an event in the application, the two requirements are not at the same level.

Logon is an event of the upper layer, which can be independent and queried. It is an event of the lower layer and cannot be independent.

From the technical point of view, login and logout can not only use the Weibo SDK, but also use the Weibo API. That is to say, the two also use Weibo, therefore, it is necessary to use a weibofacade layer to encapsulate specific communication and data parsing, which can be divided into SDK encapsulation and API encapsulation.

Let's take a look at the above assumptions. We can split them into the following parts: 1. Check friends, 2. filter, 3. Check Weibo, and 4. filter. Among them, 1 and 3 need to call the Weibo API, so our program can directly call weibofacade. There is one thing that cannot be ignored here, that is, the parameters received by the API and the returned data. Some people call it VO or DTO. In short, this data object, it must be defined in the package of Weibo, rather than in the external code logic. The direct consequence of such data structure leakage is that the module cannot be reused, and the semantics of this model is easily coupled with some semantics in the program business.

Steps 2 and 4 are completely related to business requirements. That is to say, they cannot be written in the package of Weibo, and the four things of 1234 constitute a use case, therefore, a requestservice (or other) is required to encapsulate these use cases. To meet this requirement, define a method in this service, among them, 13 two steps call weibofacade of Weibo, and 24 two parts are directly written in this method.

As a result, the business logic and third-party libraries are separated from each other. The encapsulation package of third-party libraries should be placed on the infrastructure layer. However, this still faces an extreme problem, that is, once the API of a third-party library changes (especially when the parameter changes, this situation is common in some WebService development ), it is very likely that requestservice has problems with weibofacade calls. That is to say, it is necessary to isolate the two layers so that lower-layer changes have minimal impact on the upper layer and provide some fault tolerance mechanisms, one solution is to add an adapter layer on the weibofacade layer, or a coordination layer, as the underlying exception boundary.

In short, the general idea is service-|-adapter-facade-third-party.

The previous work was not complicated due to time reasons and requirements, and it did not implement a set of good encapsulation. It directly defined a service and wrapped all the calls to Weibo using methods, it just plays a simple isolation role.

Analysis of third-party library integration methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.