DEV DAIRY | PART 1

Welcome to the first episode of our Dev Diary series! At 14 Floor Studios, we’re thrilled to share the journey behind creating our custom injection macro component. This component is designed to streamline dependency management in iOS apps, making your code more maintainable, modular, and testable.

The Vision

Our goal was simple: build a versatile and efficient tool that can be easily integrated into any iOS project. We aimed for a clean and clear codebase, emphasizing best practices such as avoiding force unwraps and embracing optionals. The result is a robust and flexible dependency injection system that we’re excited to introduce to the iOS developer community.

Design and Implementation

Let’s dive into the technical details. Our custom injection macro is built around a few core principles: easy dependency management, enhanced testability, and modularity. Here’s a look at how we implemented these features.

Creating the Injected Property Wrapper

The @Injected property wrapper is the heart of our component. It allows you to inject dependencies directly into your classes or structs

This wrapper leverages key paths to dynamically access and modify dependencies within the InjectedDependencies structure.


Managing Dependencies with Injected Dependencies

The InjectedDependencies structure provides centralized access to all dependencies in your app. This design promotes a decoupled architecture, making your code more modular and easier to test.


Defining the Injection Key

To inject a dependency, you first need to define an InjectionKey. This key acts as an identifier for the dependency you want to manage.


Usage in Your App

Integrating this dependency injection system into your app is straightforward. Here’s how you can set up and use it:

1.Define a Dependency Injection Key

Create a struct conforming to InjectionKey and provide a default value for your dependency.

2.Extend InjectedDependencies

Add a computed property to InjectedDependencies to access your dependency.

3.Inject Dependencies in Your Classes or Structs

Use the @Injected property wrapper to inject dependencies where needed.

Example in Action

Here’s a complete example demonstrating the setup and usage of this dependency injection system:

// Define the dependency key

public struct UserSessionKey: InjectionKey {

public static var currentValue: UserSession = UserSession()

}

// Extend InjectedDependencies

extension InjectedDependencies {

var session: UserSession {

get { Self[UserSessionKey.self] }

set { Self[UserSessionKey.self] = newValue }

}

}

// Use the @Injected property wrapper

class SomeService {

@Injected(\.session) var session: UserSession

func performAction() {

// Use the injected session

session.doSomething()

}

}

// Set the dependency value

InjectedDependencies[UserSessionKey.self] = UserSession(mockData: true)

// Use the service

let service = SomeService()

service.performAction()

Conclusion

This is just the beginning of our journey into custom component development. Our dependency injection system is designed to be a valuable tool in your iOS development toolkit, promoting cleaner, more maintainable code. Stay tuned for more episodes in our Dev Diary series as we continue to explore and share our development process at 14 Floor Studios.

Creating the Injected Property Wrapper

Managing Dependencies with Injected Dependencies

Defining the Injection Key

1.Define a Dependency Injection Key

2.Extend InjectedDependencies

3.Inject Dependencies in Your Classes or Structs

DOWNLOAD NOW

Injection Key
Sale Price: $2.99 Original Price: $50.00