Wednesday, October 1, 2008

Dependency Injection in XNA

The benefits of using Dependency Injection/IoC are more or less obvious, for an "academic" argument search the internets. Anyway, I started coding using the "Manager classes with static methods" style seen in most of the XNA tutorials and pretty soon my code became "spaghetized" and I realized that I really missed having an IoC container. After a few searches it appeared that I would have to make one - I would have used or Castle or Unity or whatever but all of them seem to be using Reflection.Emit which is not available on the Xbox360. Plus they just do way to much for what a game needs.

Code is not ready for public consumption just yet, but I'll clean it up and publish as soon as I can.

Anyway, it's called Radiality and it has all the features you would expect from an IoC container, life cycle management, singletons, prototypes, auto-wiring, late injection, look up by interface or name and a few more thing I forgot about.

Here is what it looks like in action.

1 comment:

Cygon said...

There is also Ninject, a quite well-written IoC container that supports the .NET Compact Framework and thus can be easily configured to build for XNA 3.x on the XBox 360 (using Reflection.Emit on the PC and plain invoke via Reflection on the XBox 360).

I have a small example project that demonstrates running Ninject on the 360 here: Using Dependency Injection in XNA

Thanks for the Radiality container. More choice is always a good thing! :)