Monday, June 09, 2008

Real Life Dependency Injection

When making a framework you come across the question of how to instantiate your objects, and how the users of your framework can inject object in the framework. Often you want it to be flexible enough for it to be easy to inject object into, but fluent enough for the programmers not to notice the low dependency barrier.

What we did was to make a static application class with a reference to a configuration object which is only loaded once at application start. The application class initiates the configuration object by running a xaml file through the xaml parser. This is a very pragmatic solution that can be used for initialization of static objects in the application. Everything is hooked together with very little code and it can easily be extended to support more objects etc. in applications using the framework.

When the user needs access to any of the injected object he can do like this:

Application.Config.DaoFactory


Although this only supports "singleton" objects it gives me the flexibility I was looking for.

1 comment:

  1. If you crave a little more inversion of control, I can recommend the Windsor Container.
    http://castleproject.org/container/index.html

    I've used it a couple of times and works like a charm :)

    ReplyDelete