I tried SubCut to resolve dependency of components in Scala. However I think The Cake Pattern might be better than SubCut because it's so complex more than necessary and not transparent.It's similar to the service locator, not the DI container. I need a DI container which is more simple and intuitive. It's sufficient that supports constructor injection.
There are PicoContainer in Java World. So I wrote a simple wrapper of PicoContainer for Scala:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.picocontainer.DefaultPicoContainer | |
/** | |
* Simple wrapper of PicoContainer for Scala. | |
* | |
* {{{ | |
* val pico = new Pico() | |
* .add[Apple] | |
* .add[Juicer] | |
* .add[Peeler] | |
* | |
* val peeler = pico.get[Peeler] | |
* }}} | |
*/ | |
class Pico { | |
val pico = new DefaultPicoContainer(); | |
def add[T](implicit m: scala.reflect.Manifest[T]): Pico = { | |
val clazz = m.erasure.asInstanceOf[Class[Any]] | |
pico.addComponent(clazz) | |
this | |
} | |
def add(c: Any): Pico = { | |
pico.addComponent(c) | |
this | |
} | |
def get[T](implicit m: scala.reflect.Manifest[T]): T = { | |
val clazz = m.erasure.asInstanceOf[Class[Any]] | |
pico.getComponent(clazz).asInstanceOf[T] | |
} | |
} |
It does not cover all features of PicoContainer. But it might be enough for my current use :-)
2 件のコメント:
Nice wrapper, but PicoContainer would be even more practical in Click Framework. Would love to use it there instead of Spring, but like most users I wasn't able to integrate it smoothly.
There were even several requests on the forums regarding Pico integration in Click.
I know some Click users requested PicoContainer integration. Previously, it had also been raised on JIRA:
https://issues.apache.org/jira/browse/CLK-384
I like PicoContainer because it so simple and lightweight like Click. I often use Guice than PicoContainer in Java recently but I'm interested in PicoContainer integration in Click. Why cat't you integrate it smoothy?
コメントを投稿