« Return to Thread: Dependency injection in Scala?

Re: Dependency injection in Scala?

by Alex Boisvert-3 :: Rate this Message:

Reply to Author | View in Thread

On 2/1/08, Stepan Koltsov <stepan.koltsov@...> wrote:
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?

Hi Stepan,

I see two sides to this story,
-How to structure code to make it more configurable? (parametric)
-How to wire parts together and provide configuration

Scala's type system and concise syntax help you on both aspects compared to Java.  I've posted a small example yesterday [1].

Similarly to Java and other OO programming languages without open classes, you want to reduce as much as possible static linking inside your application.  This typically implies two practices.  First, using object factories instead of calling constructors directly; and second, minimizing the use of singleton objects.  These practices apply to any part or behavior that you want to be able to reconfigure.  It's okay to rely on static linking for things that you don't expect (or want to) change.   The "Cake Pattern" paper [2] discusses some of these considerations.

In practice, I've found that using Scala scripts (using the interpreter) was a better solution than Spring to wire pieces together and inject configuration parameter inside my applications.   There are some issues with using Spring since Scala conventions for class naming, method naming (JavaBeans-style setters/getters) and overall application structuring tend to differ from Java.  Some simple things work but you'll run into issues with most complex scenarios.   As far as I know, nobody has tried to resolve these issues or is working on a Scala dependency-injection framework.

Hope this helps,
alex

[1] http://www.nabble.com/Cake-pattern-and-dependency-injection-td15199303.html#a15208024
[2] http://lamp.epfl.ch/~odersky/papers/ScalableComponent.pdf

 « Return to Thread: Dependency injection in Scala?