top of page
Search

Breaking Down Monolithic Code

  • sergiusuru
  • Jul 11, 2024
  • 3 min read

In this article, I'll show you how to break monolithic code into separate frameworks. This approach allows you to reuse frameworks across multiple projects without rewriting the same code repeatedly.


Getting Started with Framework Migration

We'll use a project named TestPlaces as our template. The app already has a Libraries framework integrated, along with UI and API modules. Our goal is to migrate the UI and API modules into separate frameworks. Here's the initial project structure:

ree


Approaches to Framework Migration


There are several ways to break modules into separate frameworks:

  1. Create Targets in the Same Project: Create multiple targets within the same Xcode project, where each target represents a separate framework. This allows for managing different modules within the same project.

  2. Create Framework SDK: A higher-level framework that encapsulates and manages multiple lower-level frameworks or modules. This helps in organizing and distributing related frameworks as a single package.

  3. Create Framework Packages: Using Swift Package Manager or CocoaPods, you can create separate framework packages for each module. This simplifies dependency management and allows for independent versioning and distribution.

  4. Creating Dynamic Frameworks: Dynamically linked frameworks can be loaded at runtime, allowing for better code isolation and sharing across multiple projects.

  5. Using Carthage: Carthage is another dependency manager that allows you to distribute your modules as separate frameworks.

Each method has its pros and cons. Choosing the right approach depends on project complexity, scalability, dependency management requirements, and future maintenance considerations.


We will use option: Creating a Framework SDK

Given our requirements:

  • Create a framework similar to "Libraries".

  • Keep all classes inheritable and extensible.

  • Do not use third-party libraries.


We'll use option 2: creating a framework SDK. In our code base, the UI module classes inherit from the Libraries framework, and the API module contains object types used in both the main app and the UI module. Here's how we'll create and link the frameworks to avoid dependency cycles.


Creating the UI Framework

  1. Create Framework SDK:

  • In Xcode, go to File > New > Project.

  • Select iOS as the platform and choose Framework as the template. Click Next.

  • Name the project PlacesUI and select a location to save it. It's recommended to save it in the "TestPlaces" folder to keep it consistent with the "Libraries" framework. Make sure to add it to the TestPlaces project and select the appropriate group. Click Create.

ree

2. Move UI Module Classes:

  • Move the UI module classes from the TestPlaces project to the new PlacesUI framework. The PlacesUI framework is now ready to use.


Creating API Framework

Repeat the same process for the API module, naming it PlacesAPI.

Final result is:


ree

Linking Frameworks


Now, let's link the frameworks to enable intercommunication (PlacesAPI, PlacesUI, Libraries) within the TestPlaces target. This is done to avoid dependency cycles.


Linking Steps:

  1. TestPlaces Target:

  • In Build Phases > Link Binary With Libraries, add PlacesAPI, PlacesUI, and Libraries frameworks. This means the TestPlaces target depends on these frameworks and can use their classes.

2. PlacesUI Framework:

  • In Build Phases > Link Binary With Libraries, add PlacesAPI and Libraries frameworks.

3. Libraries Framework:

  • In Build Phases > Link Binary With Libraries, add the PlacesAPI framework.


In the PlacesAPI framework, we have TSPlaceViewModel, which is used in multiple locations. By adding it to PlacesAPI, you only need to import PlacesAPI into the main app and other locations where it's used.


Final Architecture

The TestPlaces app architecture, illustrating the communication layers:

ree

Conclusion

By following these steps, we created and added framework interdependencies, ensuring our app runs as it did before migration. When migrating code into separate frameworks, analyze the code structure and how objects communicate. Based on this analysis, choose the best modularization approach that fits your project's scalability needs.

This structured approach ensures maintainable and reusable code, making your projects more efficient and easier to manage.


 
 
 

Recent Posts

See All
SwiftUI Bootcamp

Apple is indeed pushing SwiftUI as the future of iOS development because of its powerful features and ease of use. If you're wondering...

 
 
 

Kommentare


Copyright 2024 mateses 

bottom of page