« Return to Thread: Dependency injection in Scala?

Re: Dependency injection in Scala?

by Matt Hellige :: Rate this Message:

Reply to Author | View in Thread

I agree with other replies, but in a more guarded way. I think the
need for a DI framework is somewhat reduced in Scala, as compared to
Java. But I definitely don't believe that the need is completely
eliminated. There are many things that Spring does that are not easy
to achieve without a framework. So I do advise that you look into the
patterns that people mentioned, but I also expect that people will
continue to use Spring/Guice/etc. with Scala and that eventually we
will see a new approach to DI that is a more natural fit with Scala's
naming conventions and language features.

Matt

On Feb 1, 2008 11:28 AM, Alex Boisvert <boisvert@...> wrote:

>
> 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



--
Matt Hellige / matt@...
http://matt.immute.net

 « Return to Thread: Dependency injection in Scala?