Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What preferred way to wire dependencies using IoC container?

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?

like image 849
Vadim Avatar asked May 15 '09 12:05

Vadim


1 Answers

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]

like image 128
Patrick Cornelissen Avatar answered Sep 27 '22 16:09

Patrick Cornelissen