Skip to Content
Course content

249: Feature Modules and Micro-App Architecture

Click on the "Edit" button in the top corner of the screen to edit your slide content.

You've likely reached a point in your project where the "Main App" target is becoming a behemoth. When you hear senior engineers talk about "Modularization" or "Micro-App Architecture," the most common misconception I see is the belief that modularization is simply a way to organize files.

Many developers think that if they just move their PaymentViewModel and PaymentView into a separate Swift Package or Framework, they've "modularized" their app. They treat the module like a fancy folder. But here is why that's wrong: if your new PaymentModule still imports AppCore, and AppCore imports PaymentModule to initialize the flow, you haven't actually decoupled anything. You've just created a distributed monolith with slower compile times because Xcode now has to manage the overhead of a framework boundary without any of the architectural benefits.

Stop treating modules like folders; start treating them like contracts

True micro-app architecture isn't about where the code lives, but about who is allowed to know about whom. In a monolithic app, every class can see every other class. In a feature-module architecture, we enforce a strict dependency graph.

Let's take a real example: an e-commerce app. You have a CartFeature and a ProductDetailFeature. The ProductDetail screen needs to call a function to "Add to Cart." If the ProductDetailFeature imports the CartFeature directly, you're fine—until the CartFeature needs to link back to a product's detail page. Suddenly, you have a circular dependency, and the compiler will refuse to build.

The fix is to stop importing implementations and start importing Interfaces. I personally swear by the "API Module" pattern. Instead of one CartFeature module, you split it into two: CartAPI and CartImplementation.

  • CartAPI: Contains only protocols and simple data models (e.g., protocol CartServiceProtocol { func add(product: Product) }). It has zero dependencies.
  • CartImplementation: Imports CartAPI and actually does the work.

Now, the ProductDetailFeature only imports CartAPI. It doesn't know how the cart works; it just knows that something exists that conforms to CartServiceProtocol. This is the core of Micro-App architecture: features depend on abstractions, not on other features.

Managing the "Glue" with Dependency Injection

You might be wondering, "If ProductDetailFeature only knows about a protocol in CartAPI, how does it actually get a working instance of the cart service at runtime?"

This is where the Main App target comes back into play. The Main App is the "Composer." It's the only place in the entire project that is allowed to import everything. It imports both CartImplementation and ProductDetailFeature, and it "injects" the implementation into the feature.

// Inside the Main App (The Composer)
let cartService = CartService() // From CartImplementation
let productDetailVM = ProductDetailViewModel(cartService: cartService) // Injected into ProductDetailFeature

I've found that this approach does more than just fix compile times. It changes how you think. When you're forced to define a CartAPI, you're forced to think about the minimum surface area the rest of the app needs. You stop leaking internal logic and start building a system of pluggable components.




📋 Practical Task

Implementing a Decoupled Feature Interface

You are working on a travel app. Currently, the BookingFeature is tightly coupled to the UserAccountFeature, creating a circular dependency because the account page needs to show recent bookings, and the booking page needs user profile data.

Your Task: Refactor the relationship using the API Module pattern. Create the following structure in your conceptual workspace:

  • UserAccountAPI: Create a protocol UserAccountProvider with a method func getCurrentUser() -> User.
  • UserAccountImplementation: Create a class UserAccountService that conforms to UserAccountProvider.
  • BookingFeature: Create a BookingViewModel that requires an object conforming to UserAccountProvider in its initializer. It should not import UserAccountImplementation.
  • Main App: Write the initialization code that instantiates UserAccountService and passes it into the BookingViewModel.

Ensure that if you were to delete the UserAccountImplementation module, the BookingFeature would still compile (as long as the API module remained).

Rating
0 0

There are no comments for now.

to be the first to leave a comment.