views:

143

answers:

3

I understand basically how IoC frameworks work, however one thing I don't quite get is how code-based config is supposed to work. With XML I understand how you could add a new assembly to a deployed application, then change the config in XML to include it. If the application is already deployed (i.e., compiled in some form) then how can code changes be made without recompiling? Or is that what people do, just change config in code and recompile?

+2  A: 

IoC and Dep injection can help allow changes without recompiling (depending on the tools used), but don't require it. Using code to configure is about configuring the container, not about post-deployment changes. Yes, if you make the change in code you normally need to recompile.

Philip Rieck
A: 

Just because a DI container uses code for configuration doesn't mean that no configuration can be changed without recompiling. It does require that you think about exactly what you want to be configurable that way and provide some means of changing that configuration (such as through a properties file).

ColinD
+4  A: 

Hot-swapping dependencies is not the only goal of using a DI Container.

Dependency Injection (DI) is a principle that helps us develop loosely coupled code. Loose coupling only means that we can vary consumer and service independently of each other. How we achieve this is not addressed at this level.

DI Containers are frameworks that help use wire dependencies together. They are more or less just utility libraries that help us apply DI patterns. Once again, how we configure a container is perpendicular to how we consume those dependencies.

XML configurations allows us to change the container configuration without recompilation. Code as configuration doesn't.

However, swapping dependencies without recompilation is typically only relevant for a small subset of all your loosely coupled code. For the rest, a convention-based approach is much more effective, because it tends to be less brittle. See here for more information.

Mark Seemann