I am building out a new Windows Phone 7 application and this time I am finally going to use the Caliburn Micro framework (I have been telling Rob I was going to for a long, long time). Of course one of the great features of this framework is that it was built from the beginning to support DI (Dependency Injection) with its own built in, light DI container.
When setting up the container you have the ability to provide keys for the items which you register. However, one thing I was not aware of (it may be documented someplace) is that not all items in the container can/should have keys. I have a ViewModel which I have registered using the standard way (seen below)
container.RegisterPerRequest( typeof( DashboardViewModel ), "DashboardViewModel", typeof( DashboardViewModel ) );
and this View Model needed a dependency injected into it (an IDashboardService). In order to inject this IDashboardService I created the line below:
container.RegisterPerRequest( typeof( IDashboardService ), "MlbDashboardService", typeof( MlbDashboardService ) );
However when I ran the application I received a NullReferenceException inside SimpleContainer.ActivateInstace. At first I thought I did not have the interface/concretes setup correctly inside the container but after a bit of double checking I was able to verify I did. After a bit of debugging and walking though the code (not to mention pulling out more of my hair, which by the way I need to keep) I realized that the cause was that I have my IDashboardService a key. After realizing this I changed my registration line to be
container.RegisterPerRequest( typeof( IDashboardService ), null, typeof( MlbDashboardService ) );
Once I made this change and re-ran my application I was able to have my ViewModel created correctly and have the IDashboardService injected as expected.
Now I do not know if this behavior is by intent or what but since I racked my brains trying to figure out what was causing the issues I figured I would share my thoughts just in case others where having the same issue.
Till next time,
04-10-2011 7:59 AM