Dependency Injection: Basics
Image credit: stackify.com

Dependency Injection: Basics

What is It- Dependency Injection???

Literally: Ability to inject a dependency into an enclosed environment.?

This statement does remind me of my first relationship, I knew what to do but later realized the important thing was how, Could not save that one but lets see if I could help you to save your code.

Why?

The whole idea of dependency injection is do make the code independent of the dependencies, it does so by making the decoupling the usage of an object from it's creation. This helps you follow SOLID's dependency inversion and Single responsibility principle.


Before we dive into how we can do it, let's look at some code

class UserService { 
? let userRepository: UserRepository = UserRepository()
??
? func saveUser(user: User) {
? ? userRepository.save(user)
? }
}
        

What is wrong with this code, well at first glimpse it surely looks good and functional and that it partially true as it functional for sure but I would not say the same about the quality of code.

The UserService class is dependent and coupled with UserRepository. That prevents you from replacing the implementation of the interface with a different one.

Lets look into ways to solve this with dependency injection techniques.

Lets talk about how then

There are multiple ways to do dependency injection

  1. Constructor Injection: Before I start explaining how could this be solved have a check on the code which uses constructor injection.

class UserService {
  let userRepository: UserRepository
  
  init(userRepository: UserRepository) {
    self.userRepository = userRepository
  }
  
  func saveUser(user: User) {
    userRepository.save(user)
  }
}        

What you see above is called as constructor injection, it would be rhetorical to say why but still for the people not acquainted to swift programming, init is the constructor for class UserService and the dependency of userRepository is being provided as part of UserService initialization.

let userRepository = UserRepositoryImpl()
let userService = UserService(userRepository: userRepository)

let user = User(firstName: "John", lastName: "Doe", email: "[email protected]")
userService.saveUser(user: user)        

2. Property Injection: Same problem can be resolved with another injection approach called as property injection, the property to be injected is exposed to the called and is then set as a property, you will get it clearly by looking the code below


class UserService {
  var userRepository: UserRepository?

  func saveUser(user: User) {
    userRepository?.save(user)
  }
}        

let userRepository = UserRepositoryImpl()
let userService = UserService()
userService.userRepository = userRepository //proeprty injection

let user = User(firstName: "John", lastName: "Doe", email: "[email protected]")
userService.saveUser(user: user)        

3. Method Injection: This is another resolution to the problem, the dependency is injected as part of method call

class UserService {
  func saveUser(userRepository: UserRepository, user: User) {
    userRepository?.save(user)
  }
}        

let userRepository = UserRepositoryImpl()
let userService = UserService()

let user = User(firstName: "John", lastName: "Doe", email: "[email protected]")
userService.saveUser(userRepository: userRepository, user: user)
        


Well this is how far I will be going with the basics of dependency injection.

Happy Coding!!!

要查看或添加评论,请登录

Deepak Singh的更多文章

  • Concurrency in Swift...

    Concurrency in Swift...

    If you haven't read the first piece in this series, please do. Since you've seen how to create any method async and…

  • Try and Fail in Swift

    Try and Fail in Swift

    Before you start searching for 'Fail' as a keyword in Swift, it's important to know that there isn't an actual 'Fail'…

  • Dynamic dispatch vs Final

    Dynamic dispatch vs Final

    When working with classes in Swift, it gives you ability to override superclass methods and properties, which basically…

  • Concurrency in Swift

    Concurrency in Swift

    Swift has built-in support for writing asynchronous and parallel code in a structured way. Asynchronous code can be…

  • iOS meetup : Vadodara

    iOS meetup : Vadodara

    Hello iOS brain, I am an iOS developer. I am planning to organise a MEETUP for iOS developer.

    2 条评论

社区洞察

其他会员也浏览了