If you want isolate parts of your application by giving each isolated part a different container instance (by effectively making each area a module), you will have to intercept the creation of controllers and dispatch that to the container of the correct area.
Intercepting the creation of controllers is done by implementing a custom IControllerFactory. Such implementation might look like this:
public class PerAreaControllerFactoryComposite : IControllerFactory {
private IDictionary<string, IControllerFactory> areaControllerFactories;
public PerAreaControllerFactoryComposite(
IDictionary<string, IControllerFactory> areaControllerFactories) {
this.areaControllerFactories = areaControllerFactories;
}
public IController CreateController(RequestContext requestContext,
string controllerName) {
var factory = this.FindFactory(requestContext);
var controller = factory.CreateController(requestContext, controllerName);
HttpContext.Current.Items["Factory_" + controller.GetType().Name] = factory;
return controller;
}
public void ReleaseController(IController controller) {
var factory = HttpContext.Current.Items["Factory_" + controller.GetType().Name]
as IControllerFactory;
factory.ReleaseController(controller);
}
public SessionStateBehavior GetControllerSessionBehavior(
RequestContext requestContext, string controllerName) {
var factory = this.FindFactory(requestContext);
return factory.GetControllerSessionBehavior(requestContext, controllerName);
}
private IControllerFactory FindFactory(RequestContext context) {
string area = context.RouteData.DataTokens["area"].ToString();
return this.areaControllerFactories[area];
}
}
This IControllerFactory implementation accepts a dictionary with a collection of IControllerFactory implementations, where the key of the dictionary is the name of the area. When CreateController is called, it will determine the current area and forward the call to the IControllerFactory implementation for that area.
You can create this implementation as follows:
var factory = new PerAreaControllerFactoryComposite(new Dictionary
{
{ "Area1", new UnityControllerFactory(area1Container) },
{ "Area2", new UnityControllerFactory(area2Container) },
{ "Area3", new UnityControllerFactory(area3Container) },
});
ControllerBuilder.Current.SetControllerFactory(factory);
The Unity integration package probably lacks a UnityControllerFactory, but creating one will be trivial (you can simply derive from DefaultControllerFactory).
Do note that running isolated modules in one application comes with extra development overhead, so the size of the application must justify it. Having multiple container instances complicates your registration so it is typically preferable to not do this.