Log in

Subscribe to site updates.

Guidance for Introducing Dependency Injection into Existing Applications

Posted by Nicholas Blumhardt | February 04, 2008 20:01

Much like you can never go back to datasets from a good ORM, working without dependency injection seems tedious after building DI applications. Getting DI into an existing app should be a great way to move the codebase forward incrementally, but there isn't much guidance out there on how to do it.

Many developers in a position to set the architectural direction of large projects are already dealing with significant existing codebases, and I think this is one of the reasons that DI is still a relatively under-utilised technology in .NET applications.

I've started an article over on the Autofac wiki that will hopefully grow to include this sort of advice - for now the main points I think need to be addressed are:

Autofac has a couple of features that I think will make it the container of choice for this situation, simply because lambdas are so flexible. Wrapping a typical static factory method is trivial:

builder.Register(c => MyFactory.CreateProduct()).WithScope(InstanceScope.Factory);

With that the job is done and you can move on. The same thing can always be achieved in XML configuration, but little complications that come from the environment or the need to pass parameters could turn this into a headache quickly.

The other guidance that I think would be useful is a catalogue of simple patterns and/or refactorings to help with the task, e.g. creating an instance-based adapter for classes exposing static methods.

As you would have seen if you followed the article link, the page really is a shell so far. If you have any tales from the trenches or tips that you think could help someone tackling DI in a large scale existing project, it would be great to hear from you!

Comments

There are no comments on this article.

Your Comment





Reset

Disclaimer: These articles represent the opinions of the authors and may not match the official position of Ubik Systems Pty. Ltd. Confirmation should be sought on all matters involving professional advice.