Component Based Dependency Injection in Scala

112 views
Skip to first unread message

John Sullivan

unread,
Feb 13, 2013, 5:45:28 PM2/13/13
to scala...@googlegroups.com
Hi people! I hope this isn't too inappropriate for this group, but I've been using the "cake pattern" for dependency injection with Scala for the past couple years, and I wrote up my experiences using it. I hope there is some practical advice here that goes beyond the two seminal papers by Odersky and Bonér.

http://scabl.blogspot.com/2013/02/cbdi.html

Best, John

mar

unread,
Feb 13, 2013, 7:28:01 PM2/13/13
to scala...@googlegroups.com
While I've only skimmed so far this looks really interesting, thanks for sharing!

A question parallel to the subject, if you don't mind: I've seen the form "self: SelfType => ..." in other examples that also makes no obvious use of the named 'self' reference. Are there any benefits over the anonymous form "this: SelfType => ..." in these cases that I may have missed?

Naftoli Gugenheim

unread,
Feb 13, 2013, 7:55:57 PM2/13/13
to mar, scala-user
Only if you need an identifier. For instance if you have an inner class, and you need to refer to the outer class instance, you can write OuterClass.this, or the self type identifier.



On Wed, Feb 13, 2013 at 7:28 PM, mar <tekn...@gmail.com> wrote:
While I've only skimmed so far this looks really interesting, thanks for sharing!

A question parallel to the subject, if you don't mind: I've seen the form "self: SelfType => ..." in other examples that also makes no obvious use of the named 'self' reference. Are there any benefits over the anonymous form "this: SelfType => ..." in these cases that I may have missed?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



John Sullivan

unread,
Feb 13, 2013, 8:04:17 PM2/13/13
to Naftoli Gugenheim, mar, scala-user
That's interesting, thanks. I just avoid "this" here because it is a term with special meaning in the language, and so I prefer not to use it in a "non-special" way, to avoid confusion. Thanks, John

Eric Kolotyluk

unread,
Feb 13, 2013, 9:04:23 PM2/13/13
to scala...@googlegroups.com
I would be fascinated to see if Scala has some solutions to the
boilerplate problems you point out.

Cheers, Eric

On 2013-02-13 2:45 PM, John Sullivan wrote:
> Hi people! I hope this isn't too inappropriate for this group, but
> I've been using the "cake pattern" for dependency injection with Scala
> for the past couple years, and I wrote up my experiences using it. I
> hope there is some practical advice here that goes beyond the two
> seminal papers by Odersky and Bon�r.
>
> http://scabl.blogspot.com/2013/02/cbdi.html
>
> Best, John

John Sullivan

unread,
Feb 13, 2013, 9:11:00 PM2/13/13
to Eric Kolotyluk, scala-user
Me too. I would like to be able to define an interface and a default implementation of that interface in a single pass. (As I mention in the paper.) Why not? Or some operation like:

class FooImpl {
  // ...
}

trait Foo asImplementedBy FooImpl {
  // every public member of FooImpl becomes an abstract public member of this trait
}



On Wed, Feb 13, 2013 at 9:04 PM, Eric Kolotyluk <eric.ko...@gmail.com> wrote:
I would be fascinated to see if Scala has some solutions to the boilerplate problems you point out.

Cheers, Eric


On 2013-02-13 2:45 PM, John Sullivan wrote:
Hi people! I hope this isn't too inappropriate for this group, but I've been using the "cake pattern" for dependency injection with Scala for the past couple years, and I wrote up my experiences using it. I hope there is some practical advice here that goes beyond the two seminal papers by Odersky and Bonér.


http://scabl.blogspot.com/2013/02/cbdi.html

Best, John
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.

Clint Gilbert

unread,
Feb 13, 2013, 10:38:51 PM2/13/13
to scala...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I've often wanted the same thing. Perhaps it could be available for
every type?

class SomeClass {
def foo
def bar
}

class Blarg extends SomeClass.Api { ... }

or maybe apiOf[T], or contract[T]?

Are the latter something macros could accomplish?

On 02/13/2013 09:11 PM, John Sullivan wrote:
> Me too. I would like to be able to define an interface and a
> default implementation of that interface in a single pass. (As I
> mention in the paper.) Why not? Or some operation like:
>
> class FooImpl { // ... }
>
> trait Foo asImplementedBy FooImpl { // every public member of
> FooImpl becomes an abstract public member of this trait }
>
>
>
> On Wed, Feb 13, 2013 at 9:04 PM, Eric Kolotyluk
> <eric.ko...@gmail.com <mailto:eric.ko...@gmail.com>>
> wrote:
>
> I would be fascinated to see if Scala has some solutions to the
> boilerplate problems you point out.
>
> Cheers, Eric
>
>
> On 2013-02-13 2:45 PM, John Sullivan wrote:
>
> Hi people! I hope this isn't too inappropriate for this group, but
> I've been using the "cake pattern" for dependency injection with
> Scala for the past couple years, and I wrote up my experiences
> using it. I hope there is some practical advice here that goes
> beyond the two seminal papers by Odersky and Bon←r.
>
> http://scabl.blogspot.com/__2013/02/cbdi.html
> <http://scabl.blogspot.com/2013/02/cbdi.html>
>
> Best, John -- You received this message because you are subscribed
> to the Google Groups "scala-user" group. To unsubscribe from this
> group and stop receiving emails from it, send an email to
> scala-user+unsubscribe@__googlegroups.com
> <mailto:scala-user%2Bunsu...@googlegroups.com>. For more
> options, visit https://groups.google.com/__groups/opt_out
> <https://groups.google.com/groups/opt_out>.
>
>
>
> -- You received this message because you are subscribed to the
> Google Groups "scala-user" group. To unsubscribe from this group
> and stop receiving emails from it, send an email to
> scala-user+unsubscribe@__googlegroups.com
> <mailto:scala-user%2Bunsu...@googlegroups.com>. For more
> options, visit https://groups.google.com/__groups/opt_out
> <https://groups.google.com/groups/opt_out>.
>
>
>
> -- You received this message because you are subscribed to the
> Google Groups "scala-user" group. To unsubscribe from this group
> and stop receiving emails from it, send an email to
> scala-user+...@googlegroups.com. For more options, visit
> https://groups.google.com/groups/opt_out.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAlEcXEkACgkQ0GFaTS4nYxs3ZQCeMarMBeP09VIFKtlcAbu6lPrj
i8YAn1BRd2UqOySUpyg2mr/QlpC2jSY2
=ZA7h
-----END PGP SIGNATURE-----

Juha Heljoranta

unread,
Feb 14, 2013, 5:24:02 PM2/14/13
to scala...@googlegroups.com, Eric Kolotyluk
On Wednesday, February 13, 2013 18:04:23 Eric Kolotyluk wrote:
> I would be fascinated to see if Scala has some solutions to the
> boilerplate problems

Cake pattern is powerful and more than just injection. I wouldn't try to use
it too lightly. My point being: choose the Right Tool for the Job.

For example, a constructor injection is less verbose:
https://www.google.com/search?q=Constructor+injection+in+Scala

Cheers,
Juha

Reply all
Reply to author
Forward
0 new messages