Top 5 Dependency Injection iOS Interview Questions
Oct 08, 2023Q1. What is dependency injection? What are the types of Dependency Injection in Swift?
Dependency Injection is simply injecting the dependencies that a class or module requires through its constructor, properties and methods, instead of creating them within the class itself.
It promotes loose coupling between components, making the code more modular, testable, and maintainable.
There are three common types of dependency injection:
Constructor Injection: This is the most common form of dependency injection implementation in Swift. It involves passing dependencies through a class's initializer (constructor) when creating an instance of that class. Here is a short example of implementing constructor injection:
class UserService {
let dataProvider: DataProvider
init(dataProvider: DataProvider) {
self.dataProvider = dataProvider
}
}
Property Injection: In property injection, dependencies are set through properties after the object is created. This allows you to change or provide the dependencies at any point during the object's lifecycle. Here is a short example of implementing property injection:
class OrderService {
var paymentService: PaymentService?
}
Method Injection: In method injection, dependencies are passed as parameters to methods when they are called. This is typically used when you have methods that require specific dependencies for a particular operation. Here is a short example of implementing method injection:
class NotificationManager {
func sendNotification(message: String, via notifier: Notifier) {
notifier.send(message: message)
}
}
Q2. What is Constructor Injection? When you should choose it?
Constructor injection involves passing dependencies as parameters to a class's initializer. It ensures that the dependencies are available when an instance of the class is created. This is the most common form of dependency injection.
Constructor injection is typically preferred for mandatory dependencies because it enforces that dependencies are provided when an object is created. It makes the dependencies explicit in the class's interface.
Let's consider an example of a view controller that needs a product ID as a dependency to display product details.
import UIKit
class ProductDetailController: UIViewController {
private let productId: String
// Constructor (Initializer) that accepts the product ID as a parameter
init(productId: String) {
self.productId = productId
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
fetchProductDetails()
}
func fetchProductDetails() {
// perform further actions
}
}
let productId = "12345"
let productDetailVC = ProductDetailController(productId: productId)
By using constructor injection, you ensure that the ProductDetailController
has access to the required product ID when it's created, making it clear what dependencies it needs and promoting a more modular and testable design.
Q3. What is Property Injection? Explain with an example.
In property injection, dependencies are provided to a class through properties after an instance of the class has been created. This is typically used for optional dependencies or when you want to allow the dependencies to be set at a later time.
Let's consider the same example of a view controller that needs a product ID for product details:
class ProductDetailController: UIViewController {
// Property to hold the product ID (injected dependency)
var productId: String?
override func viewDidLoad() {
super.viewDidLoad()
if let productId = productId {
fetchProductDetails(productId: productId)
} else {
// Handle the case when productId is not set
// Display an error message or handle it accordingly
}
}
func fetchProductDetails(productId: String) {
// Perform further actions
}
}
let productDetailVC = ProductDetailController()
productDetailVC.productId = "12345"
With property injection, you have the flexibility to set the productId
at any time after creating the object. This can be useful when you don't have all the required dependencies available at the time of object creation or when certain dependencies are optional.
Q4. When to choose Method Injection over Constructor Injection?
In Method Injection, dependencies are passed as parameters to specific methods when they are needed, rather than being injected through the class's constructor. This is typically done when a class requires certain dependencies for only a subset of its methods to perform further actions.
Let's see an example of a view controller that needs a product ID for product details with method injection:
class ProductDetailController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Usage of method injection to fetch and display product details
let productId = "12345"
fetchProductDetails(productId: productId)
}
func fetchProductDetails(productId: String) {
// Perform further actions
}
}
Method Injection can be beneficial in situations where the dependency is required for only specific methods within the class. It allows for more flexibility in handling dependencies and can be a suitable alternative to constructor injection when not all methods of the class require the same set of dependencies.
Q5. What are the benefits of adopting the Dependency Injection?
It encourages the separation of concerns and modularity. When you inject dependencies, each component of your app becomes more self-contained and focused on a specific task.
It makes your codebase more flexible and extensible. You can easily switch out one implementation of a dependency for another without modifying lots of code.
It reduces tight coupling between classes. When dependencies are injected, a class doesn't need to know how to create or manage its dependencies.
With dependency injection, components with well-defined interfaces and clear dependency requirements become more reusable. You can take a class and use it in different contexts, potentially with different implementations of its dependencies.
We hope that these interview questions will help you in your iOS interview preparation and also strengthen your basics on Dependency Injection in Swift. We have launched our new e-book "Cracking the iOS Interview" with Top 100 iOS Interview Questions & Answers. Our book has helped more than 372 iOS developers in successfully cracking their iOS Interviews.