views:

198

answers:

5

I love the concept of DI and loosely coupled system, a lot. However, I found tooling in Spring lacking at best. For example, it's hard to do "refactoring", e.g. to change a name of a bean declared in Spring. I'm new to Spring, so I would be missing something. There is no compiling time check etc.

My question is why do we want to use XML to store the configuration? IMO, the whole idea of Spring (IoC part) is to force certain creational pattern. In the world of gang-of-four patterns, design patterns are informative. Spring (and other DIs) on the other hand, provides very prescribed way how an application should be hooked up with individual components.

I have put Scala in the title as well as I'm learning it. How do you guys think to create a domain language (something like the actor library) for dependency ingestion. Writing the actual injection code in Scala itself, you get all the goodies and tooling that comes with it. Although application developers might as well bypass your framework, I would think it's relatively easy to standard, such as the main web site/app will only load components of certain pattern.

A: 

i can't really comment on scala, but DI helps enforce loose coupling. It makes refactoring large apps soooo much easier. If you don't like a component, just swap it out. Need another implementation for a particular environment, easy just plug in a new component.

bostonBob
+1  A: 

I love the concept of DI and loosely coupled system, a lot. However, I found tooling in Spring lacking at best. For example, it's hard to do "refactoring", e.g. to change a name of a bean declared in Spring. I'm new to Spring, so I would be missing something. There is no compiling time check etc.

You need a smarter IDE. IntelliJ from JetBrains allows refactoring, renaming, etc. with full knowledge of your Spring configuration and your classes.

My question is why do we want to use XML to store the configuration?

Why not? You have to put it somewhere. Now you have a choice: XML or annotations.

IMO, the whole idea of Spring (IoC part) is to force certain creational pattern. In the world of gang-of-four patterns, design patterns are informative.

ApplicationContext is nothing more than a big object factory/builder. That's a GoF pattern.

Spring (and other DIs) on the other hand, provides very prescribed way how an application should be hooked up with individual components.

GoF is even more prescriptive: You have to build it into objects or externalize it into configuration. Spring externalizes it.

I have put Scala in the title as well as I'm learning it. How do you guys think to create a domain language (something like the actor library) for dependency ingestion.

You must mean "injection".

Writing the actual injection code in Scala itself, you get all the goodies and tooling that comes with it.

Don't see what that will buy me over and above what Spring gives me now.

Although application developers might as well bypass your framework, I would think it's relatively easy to standard, such as the main web site/app will only load components of certain pattern.

Sorry, I'm not buying your idea. I'd rather use Spring.

But there's no reason why you shouldn't try it and see if you can become more successful than Spring. Let us know how you do.

duffymo
Thanks, I'll check IntelliJ's Spring refactoring support. The problem I'm having is that we have some in house extension to Spring, probably more along the line of OSGi component/packaging. In this internal paradigm, I need to wire properties among components. When you miss one wiring, no error at build time, but when the complex thing starts up, something will throw up somewhere. Diagnosing it is pretty hard. Actually just trying to figure out what is the missing property is hard. I ended up writing a tool, parsing through the spring and ant files to figure out what's missing.
Xian Xu
IntelliJ would have caught that. And if I have logging turned on, I usually get enough information where I don't have writing issues.
duffymo
+2  A: 

There's a good article on using Scala together with Spring and Hibernate here.

About your question: you actually can use annotations. It has some advantages. XML, in turn, is good beacause you don't need to recompile files, that contain your injection configs.

folone
+2  A: 

I agree! To me he way most people use Spring is a mild version of hell.

When you look at the standard Springified code there are interfaces everywhere, and you never really know what class is used to implement an interface. You have to look into some fantastic configuration to find that out. Easy read = not. To make this code surfable you need a very advanced IDE, like Intelly J.

How did we end up in this mess? I blame automated unit testing! If you want to connect mocks to each and every class you can not have dependencies. If it wasn't for unit testing we could probable do just as well without loose coupling, since we do not want the customer to replace single classes willy nilly in our Jars.

In Scala you can use patterns, like the "Cake Patten" to implement DI without a framework. You can also use structural typing to do this. The result is still messy compared to the original code.

Personally I think one should consider doing automated testing on modules instead of classes to escape this mess, and use DI to decouple entire modules. This strategy is by definition not unit testing. I think most of the logic lies in the actual connections between classes, so IMHO one will benefit more from module testing than unit testing.

olle kullberg
"...you never really know what class is used to implement an interface..." - just look in the configuration. The whole point of designing to an interface is that clients don't have to know.
duffymo
@duffymo Yes, configuration configuration and once again configuration. Am I the only one totally tired of configuration? I do not object to interfaces, but I do object to interfaces everywhere.
olle kullberg
Not everywhere - if everything is an interface, you're doing it wrong. And if you're not expressing those things in configuration, then it's in code. What makes code less tiresome? You've gotta say it somewhere.
duffymo
+2  A: 

There is an ongoing debate if Scala needs DI. There are several ways to "do it yourself", but often this easy setup is sufficient:

//the class that needs injection
abstract class Foo {
  val injectMe:String
  def hello = println("Hello " + injectMe)
}

//The "binding module"
trait Binder {
  def createFooInstance:Foo
}

object BinderImpl extends Binder {
  trait FooInjector {
    val injectMe = "DI!"   
  }

  def createFooInstance:Foo = new Foo with FooInjector
}

//The client
val binder:Binder = getSomehowTheRightBinderImpl  //one way would be a ServiceLoader
val foo = binder.createFooInstance
foo.hello
//--> Hello DI!

For other versions, look e.g. here: http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di.html

Landei
You mean "if Scala needs Spring", not "if Scala needs DI"? And that "getSomehowTheRightBinderImpl" call implies SOME kind of dependency-building framework.
Rodney Gitzel
I mean "if Scala needs a DI framework", to be precise, and as I already commented "getSomehowTheRightBinderImpl" could be realized using a ServiceLoader, which is not "real" DI and already built in. Even a class name in a property file could be good enough. We're talking about the one and only place where DI is bootstrapped from, and if this is a little bit messy it's not too bad. You don't need a framework just for this if all other DI parts already work fine (which is the case when the language is powerful enough).
Landei