My main ASP.NET MVC composite application uses a global Unity container to register types. The app sets the controller factory up to use this global container. I would like to refactor this such that each one of my portable areas leverages it's own child Unity container, so that different areas can implement interfaces in varying ways. It seems like I would need to have a different ControllerFactory per area. How would I accomplish that, given that the following sets the factory for all?
ControllerBuilder.Current
.SetControllerFactory(/* controller factory with global unity container */);
You can use a
MasterControllerFactory
that contains all theIControllerFactory
implementations for each area, and knows which factory can build whichRequestContext
. This actually allows you to select a different ControllerFactory for any variation, not just by area. Here's how it works:All of the area controller factory implementations must implement
IFilteredControllerFactory
instead ofIControllerFactory
. Here it is:An example of an implementation that filters based on the area name looks like this:
Then you need the
MasterControllerFactory
, which looks like this:In the Application_Start of your
global.asax
you still need to set everything up, but that's easy.Obviously, you can tweak this a number of ways to work best with your coding style and application architecture.