Dependency injection in Scala

1,757 views

Published on

Dependency injection in Scala done without any frameworks.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,757
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Dependency injection in Scala

  1. 1. Hvordan vi klarte oss uten Spring, dependency injection i Scala Alf Kristian Støyle Know IT Objectnet
  2. 2. Dependency Injection Transactions
  3. 3. Cake pattern ‘Cake Pattern’ because “(...) beyond myappreciation of cake, [a] cake is made of anumber of layers (separated by jam), and can be sliced. The layers represent thedifferent levels of inner class nesting. It is conceivable that you would reach the bottom layer by working your way down from the top. http://scala.sygneca.com/patterns/component-mixins
  4. 4. http://lamp.epfl.ch/~odersky/papers/ScalableComponent.pdf http://jonasboner.com/2008/10/06/real-world-scala- dependency-injection-di.html
  5. 5. case class User(username: String, password: String)
  6. 6. class UserRepository { def authenticate(user: User): User = { println("authenticating user: " + user) user } def create(user: User): User = { println("creating user: " + user) user }}
  7. 7. class UserService { val userRepository = new UserRepository def authenticate(username: String, password: String): User = userRepository.authenticate(new User(username, password)) def create(username: String, password: String): User = userRepository.create(new User(username, password))}
  8. 8. class UserRepository { def authenticate(user: User): User = { println("authenticating user: " + user) user } def create(user: User): User = { println("creating user: " + user) user }}
  9. 9. trait UserRepositoryComponent { val userRepository: UserRepository class UserRepository { def authenticate(user: User): User = { println("authenticating user: " + user) user } def create(user: User): User = { println("creating user: " + user) user } }}
  10. 10. class UserService { def authenticate(username: String, password: String): User = userRepository.authenticate(new User(username, password)) def create(username: String, password: String): User = userRepository.create(new User(username, password))}
  11. 11. trait UserServiceComponent extends UserRepositoryComponent {//val userRepository: UserRepository val userService: UserService class UserService { def authenticate(username: String, password: String): User = userRepository.authenticate(new User(username, password)) def create(username: String, password: String): User = userRepository.create(new User(username, password)) }}
  12. 12. trait UserServiceComponent { self: UserRepositoryComponent =>//val userRepository: UserRepository val userService: UserService class UserService { def authenticate(username: String, password: String): User = userRepository.authenticate(new User(username, password)) def create(username: String, password: String): User = self.userRepository.create(new User(username, password)) }}
  13. 13. object ComponentRegistry extends UserServiceComponent with UserRepositoryComponent { val userRepository = new UserRepository val userService = new UserService}val userService = ComponentRegistry.userServiceval user = userService.authenticate("user", "password")// => User(user,password)
  14. 14. import org.mockito.Mockito._class TestingEnvironment extends UserServiceComponent with UserRepositoryComponent { val userRepository = mock(classOf[UserRepository]) val userService = new UserService}val testEnv = new TestingEnvironmentwhen(testEnv.userRepository .authenticate(new User("user", "password"))) .thenReturn(new User("mock", "mockpwd"))val userService = testEnv.userServiceval user = userService.authenticate("user", "password")// => User(mock,mockpwd)
  15. 15. Gotchas• Class vs Object• Typenavn
  16. 16. Andre måter• Structural types• Implicit declarations• Functional currying• Spring• Guice
  17. 17. Transactionsimport org.springframework.transaction.annotation.Transactionalclass UserService { @Transactional def create(username: String, password: String) = { userRepository.create(new User(username, password)) }}
  18. 18. Transactionsimport TransactionManager._class UserService { def create(username: String, password: String) = transactional { userRepository.create(new User(username, password)) }}
  19. 19. object TransactionManager { def transactional[A](work: => A): A = ...}
  20. 20. Scala DI http://jonasboner.com/2008/10/06/real-world-scala- dependency-injection-di.html http://programming-scala.labs.oreilly.com/ ch13.html#DependencyInjectionInScalahttp://debasishg.blogspot.com/2011/03/pushing-envelope- on-oo-and-functional.html
  21. 21. alf.kristian@gmail.comhttp://slideshare.net/stoyle/di-scala http://github.com/stoyle/di-scala

×