Your SlideShare is downloading. ×
Dependency injection in Scala
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Dependency injection in Scala

1,426
views

Published on

Dependency injection in Scala done without any frameworks.

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,426
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Hvordan vi klarte oss uten Spring, dependency injection i Scala Alf Kristian Støyle Know IT Objectnet
  • 2. Dependency Injection Transactions
  • 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. http://lamp.epfl.ch/~odersky/papers/ScalableComponent.pdf http://jonasboner.com/2008/10/06/real-world-scala- dependency-injection-di.html
  • 5. case class User(username: String, password: String)
  • 6. class UserRepository { def authenticate(user: User): User = { println("authenticating user: " + user) user } def create(user: User): User = { println("creating user: " + user) user }}
  • 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. class UserRepository { def authenticate(user: User): User = { println("authenticating user: " + user) user } def create(user: User): User = { println("creating user: " + user) user }}
  • 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. 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. 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. 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. 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. 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. Gotchas• Class vs Object• Typenavn
  • 16. Andre måter• Structural types• Implicit declarations• Functional currying• Spring• Guice
  • 17. Transactionsimport org.springframework.transaction.annotation.Transactionalclass UserService { @Transactional def create(username: String, password: String) = { userRepository.create(new User(username, password)) }}
  • 18. Transactionsimport TransactionManager._class UserService { def create(username: String, password: String) = transactional { userRepository.create(new User(username, password)) }}
  • 19. object TransactionManager { def transactional[A](work: => A): A = ...}
  • 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. alf.kristian@gmail.comhttp://slideshare.net/stoyle/di-scala http://github.com/stoyle/di-scala