Integrating Service-Oriented Architecture in Swift 5

Tram Ho

Why just using MVC / MVVM / VIPER is not enough

Introduce

Why is it not enough just to use MVC, MVVM, MVP or VIPER ?. These architecture patterns only handle the higher level (higher level) (UI) of your application. But very often, you also have to deploy network managers (APIs), API clients, data sources, containers, and so on. It’s easier to live with your dev by structuring the interaction between high level and low level implementations.

In this article I will use a demo app on the github of my reference to help you understand the problem more easily.

Project Structure

The project root directory is divided into three directories: Classes, Resources and Supporting Files. The Resources folder contains Assets.xcassets and Supporting Files containing LaunchScreen.storyboard and Info.plist.

As you can see, in Classes, we have the following directories:

  • The ApplicationLayer folder contains the AppDelegate.swift file.
  • The PresentationLayer folder has two VIPER modules: Quotes and QuoteDetail.
  • The BusinessLogicLayer folder contains Services and Entities [also called models in MV (X)].
  • The Services directory has three services we use in the application: quoteService, fetching citations from the API; KingfisherService, retrieves image data from a URL; and ImageDataService, create an image from that data.
  • The CoreLayer folder contains all the help files we will need to work with services like: ApiUrls, configuration and network clients (network clients).

Identify Service

First, we need to provide a basic function of a service and they will be subclassed according to future specific services:

Service.swift

Each service must have a unique serviceName and register method.

By using ServiceRegistryImplementation (), we will register our service in AppDelegate.swift.

Having completed the facility, we are now ready to identify the specific services for our application.

QuoteService

This service is responsible for fetching citations from the API.

Here is how we define it:

QuoteService.swift

In the extension of quoteService, we develop the actual networking:

KingfisherService

This service is responsible for fetching image data from a URL. It is defined similarly to QuoteService:

KingfisherService.swift

In the KingfisherService extension, we download the image data:

ImageDataService

ImageDataService provides UIImage when provided Data. Yes, Next. They use a similar implementation:

ImageDataService.swift

Converting data into images is done in the extension:

Registering Services

To provide the services available for use in the application, we define the ServiceRegistryImcellenceation within AppDelegate:

AppDelegate.swift

Use Services

Let’s see how to use QuoteService to fetch the list of quotes that will be displayed in QuotesViewController:

QuotesInteractor.swift

If you’re not familiar with the VIPER architecture , you simply need to know QuotesInteractor handles all the business logic related to quotes screens, by passing the QuoteService as a dependency and calling its getQuotes method. You can call the same method inside UIViewController if you are following the MVC or ViewModel architecture in the case of MVVM.

Similarly, this is how we use KingfisherService to display character images in QuotesDetailViewController:

Using ImageDataService is quite simple but with some VIPER nuances:

In VIPER, Presenter is responsible for receiving the results returned from Interactor, preparing it as data so that it can display the UI and forward it to ViewContoder – defined in InteractorToPresenterQuoteDetailProtocol.

End

Consider checking these resources if you’re curious about how others implement service-oriented architecture: SOA in Swift , SOA-Services .

For more information, visit: https://medium.com/better-programming/implement-a-service-oriented-architecture-in-swift-5-fc70b8117616 gitthub Reference: SOA-Demo-App

Share the news now

Source : Viblo