I guess Dependency Injection isn't as widely used in the game development community as it is elsewhere, but any non-trivial game faces the same problems managing its dependencies as does any other large application. I gave it a try some time ago and have been sold ever since :)
Just a small heads-up, if you didn't come across this technique before: In concept it's like XNA's Game.Services collection through which any of the game's components can offer and/or consume services -- but it works automatically, with less code, lazy initialization and automatically resolved initialization order. Required services are simply added to a component's constructor, thus making it much clearer which services it consumes (and which ones you have to mock if you're writing unit tests).
To make it easier for me to add SunBurn into my games, I wrote some modules for Ninject, an Open Souce dependency injector. Now it's just 1 line of code to make available all of SunBurn to the whole game:
kernel.Load<SunBurnModule>();
Here's how you would instantiate and add a GameComponent to the game:
var gameComponents = kernel.Get<GameComponentCollection>();gameComponents.Add(kernel.Get<TestComponent>());
That component could request *any* service in SunBurn or XNA from Ninject by simply adding it to its constructor:
/// <summary>/// Useless game component for testing out things/// </summary>public class TestComponent : DrawableGameComponent { public TestComponent( Game game, LightingSystemManager lightingSystemManager, SceneInterface sceneInterface, SceneState sceneState, ContentManager content ) : base(game) {}}
If anyone wants to take a closer look, I've posted the full source code on my blog:
http://www.nuclex.org/blog/gamedev/99-using-ninject-with-sunburn
Wow, now that is extremely useful. Thanks. :)
Wow, awesome work - I hope you don't mind, this article is extremely handy, so I linked to it in the Recommended SunBurn Tutorials.
Great job! :)
Follow me on Twitter – development and personal tweetsAwesome XNA Videos – Lighting, Rendering, and game videos
I feel honored if you think it's worthy of being listed there :)