« Return to Thread: Dependency injection in Scala?

Re: Dependency injection in Scala?

by David Pollak :: Rate this Message:

Reply to Author | View in Thread

object HttpBuilder {
  var buildHttp: () => HttpThingIntf = new NormalHttpThing
}


// I want to change what is built by the HttpBuilder:

HttpBuilder.buildHttp = () => new MockHttpThing

And to build a new HttpThing:

val myHttpThing = HttpBuilder.buildHttp()
You can extend the paradigm with PartialFunctions:

object HttpBuilder {
  var buildHttp: PartialFunction[Unit, HttpThingIntf] = {
    case _ => new NormalHttpThing
  }
}

And you can modify the functionality with a conditional:

HttpBuilder.buildHttp = {
  case _ if someCondition => new Condition1HttpMock
  case _ if someOtherCondition => new Condition2HttpMock
} orElse HttpBuilder.buildHttp


Stepan Koltsov wrote:
What is "type-safe creator functions"?

S.

On 2/1/08, David Pollak dpp@... wrote:
  
See http://onestepback.org/articles/depinj/contents.html

In general, Scala doesn't need DI because one can use type-safe
"creator" functions.

Stepan Koltsov wrote:
    
Hello,

Is there any way to write code in Scala as in Java+Spring? I. e. with
dependency injection of components.

It is hard to write large applications without DI.

Should DI be done in pure Scala some way, or there is any framework,
or Spring itself should be used?

S.

      
    

 « Return to Thread: Dependency injection in Scala?