MVC4 custom razor base page dependency injection using unity

This is more to remind myself than anything as the docs for this are somewhat scattered around the web or are needlessly complex now mvc4 has been released.

What is it?

Sometimes you want information to be available to every view in your project regardless of it’s source (common contextual information). You have a couple of ways of doing this:

  • Inject it into every constructor action and add it to every viewmodel.
  • Have a common base class for your constructors and viewmodels and use an action filter to inject the common parts - bleugh.
  • Somehow inject into the view itself.

The solution I have gone for is option 3. The basic idea is to create a new razor base view class, wire this up in the web.config and then use unity to inject our dependencies. We can’t use constructor injection for this though as mvc does not like that at all; property injection works fine though.

The code shows this better than any explanation and is available here:

Note: this doesn’t work for layouts as they seem to exist outside of this process….very odd

But you’re calling out from the view, this is bad!!!!

I compleatly agree, calling out from the view is bad; I think in this case though that the alternatives are worse. I wouldn’t use this technique for injecting stuff that belongs in the view model but for common contextual dependancies that are used all the time it works great