views:

263

answers:

4

I believe that most IoC containers allow you to wire dependencies with XML configuration file. What are cons and pros for using configuration file vs. registering dependencies in code?

A: 

I'm assuming that by "registering dependencies in code" you mean "use new".

'new' is an extraordinarily powerful dependency injection framework. It allows you to "inject" your "dependencies" at the time of object creation - meaning no forgotten parameters, or half-constructed objects.

The other major potential benefit is that when you use refactoring tools (say in Resharper, or IntelliJ), the calls to new change too

Otherwise you can use some XML nonsense and refactor with XSL.

time4tea
yeah - whatever! spring lovers.
time4tea
Inform yourself before answering. Most containers offer alternative ways of configuration other than xml. Fluent APIs can buy you back some of the refactoring convenience. Plus, containers are more than just a DI convencience: http://ayende.com/Blog/archive/2007/10/20/Dependency-Injection-doesnt-cut-it-anymore.aspx
Mauricio Scheffer
I consider myself fairly well informed on this particular topic, thanks for the advice! I read the linked article, and still see nothing of 'framework' interest. To be honest - everything in that article sounds like poor stuff to me. However, each to their own. You can lead a horse...
time4tea
I agree. I feel that many developers miss what IOC is, before blindly going ahead and using IOC containers. I would advise to first build you application using 'new' until you feel that it gets in the way, or is no longer easy (which I've never found).
Raymond Barlow
I concur, spring/[insert your IoC container here] != DI and in taking on some framework I've often taken on their baggage at the same time. I think the Patricks answer is pretty fair... i favour coding things up but you may have to work harder to keep the wiring code neat. A added side affect though is that you may end up understanding the structure of your application better.
Toby
+2  A: 

XML pros:

  • Can change wiring and parameters without recompiling. Sometimes this is nice to have when switching environments (e.g. you can switch a fake email sender used in dev to the real email sender in production)

Code pros:

  • Can take advantage of strongly-typed languages (e.g. C#, Java)
  • Some compile-time checking (can't statically check dependencies, though)
  • Refactorable using regular refactoring tools.
  • Can take advantage of DSLs (e.g. Binsor, Fluent interfaces)
  • Less verbose than XML (e.g. you don't need to always specify the whole assembly qualified name (when talking .net))
Mauricio Scheffer
+4  A: 

These pros and cons are based on my work with spring. It may be slightly different for other containers.

XML

pro

  • flexible
  • more powerful than annotations in some areas
  • very explicit modelling of the dependencies of your classes

con

  • verbose
  • difficulties with refactoring
  • switching between several files

Annotations

pro

  • less file switching
  • auto configuration for simple wirings
  • less verbose than xml

con

  • more deployment specific data in your code (usually you can override this with xml configs)
  • xml is almopst(?) always needed, at least to set up the annonation based config
  • annotation magic may lead to confusion when searching for the class that is used as dependency

Code

pro

  • Can take advantage of strongly-typed languages (e.g. C#, Java)
  • Some compile-time checking (can't statically check dependencies, though)
  • Can take advantage of DSLs (e.g. Binsor, Fluent interfaces)
  • Less verbose than XML (e.g. you don't need to always specify the whole assembly qualified name (when talking .net))

con

  • wiring via code may lead to complex wirings
  • hard dependencies to IOC container in the codebase

I am using a mix of XML+Annotation. Some things especially regarding database access are always configured via xml, while things like the controllers or services are mostly configured via annotations in the code.

[EDIT: I have borrowed Mauschs code PROs]

Patrick Cornelissen
seems that you're missing code registrations and I'm missing annotations registrations.
Mauricio Scheffer
A: 

I concur. I have found ioc containers to give me very little, however they can very easily make it harder to do something. I can solve most of the problems I face just by using my programming language of choice, which have allways turned out to be simpler easier to maintain and easier to navigate.

Tom Malone