|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Cake pattern and dependency injectionStarting a new thread here asking if anybody has any experience with moving from
DI to using the Cake Pattern. A few people on the list have stated that this is good way to go. http://article.gmane.org/gmane.comp.lang.scala.debate/345 I haven't seen any code or documentation on this style of coding. A Google search for '"cake pattern" dependency injection' turns up nothing. Currently, we use Spring's DI for a networked Scala application and if I could get rid of DI, I would be happier for it. I guess the Cake pattern would need to be able to supply mock objects though for testing, which DI gives you. Regards, Blair -- Blair Zajac, Ph.D. <blair@...> http://www.orcaware.com/svn/ |
|
|
Re: Cake pattern and dependency injectionOn 1/30/08, Blair Zajac <blair@...> wrote:
Starting a new thread here asking if anybody has any experience with moving from trait Movie { def director: String override def toString = "Movie directed by " + director } trait MovieFinder { def findAll: List[Movie] } abstract class MovieLister { def moviesDirectedBy(director: String): List[Movie] = { finder.findAll.filter(_.director == director) } def finder: MovieFinder // dependency } class ColonDelimitedMovieFinder(val filename: String) extends MovieFinder { def findAll = { // dummy implementation List(new Movie { def director = "Steven Spielberg" }) } } In the above, I chose to use an abstract factory method "finder" as a hook to obtain the dependency. I could have used a self-type with a mix-in composition but in this contrived example it doesn't matter much and I happen to find the abstract method more concise and generally easier to reason with. It also provides an example where the full-blown "cake pattern" isn't required for dependency injection. To wire everything together and to keep configuration out of the application's code, I use a Scala script (on Unix): #!/bin/sh exec scala $0 "$@" !# val filename = args(0) val director = args(1) // Do all the wiring val lister = new MovieLister { val finder = new ColonDelimitedMovieFinder(filename) } // Do something with the application lister.moviesDirectedBy(director).foreach(println _) I find this kind of scripting much more readable and flexible than Spring's XML configuration (v1 or v2 with namespaces). And it has the benefit of being more type-safe. Here's the application in action, boisvert@boog:~/scala/movieLister$ ./movieLister.sh movies1.txt "Steven Spielberg" Movie directed by Steven Spielberg Hopefully this is useful for others. alex |
| Free embeddable forum powered by Nabble | Forum Help |