MVC Preview 2 and Unity - Stuck Registering Controllers Now?

Mar 13, 2008 at 2:37 PM
The new MVC Preview 2 was released and they changed the signature of the CreateController Method on an IControllerFactory to now pass the name of the controller to be created as a string as opposed to a type:

public IController CreateController(RequestContext context, string controllerName)

I am rather bummed out about this because I always enjoyed the ability of Unity to create a type without adding it to its container.

Now that I only have the controller name (e.g. "Home") and not the controller type, however, I assume this means I am stuck registering the controller in the container now?

Programmatically I added it like this for proof of concept:

UnityContainer.RegisterType<IController,HomeController>("Home")

I could add it via configuration and/or do some reflection tricks to look for controllers during startup, etc., but is there a more elegant approach using Unity? Any ideas are appreciated.

Best Regards,

Dave
Mar 13, 2008 at 6:28 PM
I think it's more a concern of MVC than Unity.

What happened it that they transfered the burden onto the developer to determine the controller type given the name.

If you want to wrap that logic into the Unity chain, you could insert your Controller Name - Controller Type mapping code in a builder strategy at the TypeMapping stage.

So you'll call Unity as follows:
IController controller = UnityContainer.Resolve<IController>(controllerName);

and down the chain, your brand new strategy will fetch the controllerName from the IBuilderContext.BuildKey and go on mapping to a controller type, changing the buildKey to the new one.
Mar 13, 2008 at 7:23 PM
It is more on an MVC concern, but I was hoping there was an elegant solution with Unity to avoid registering controllers.

The builder strategy sounds like an interesting thing to try, if for no other reasons, to get a bit more experience with ObjectBuilder2.

Thanks,

Dave
Mar 13, 2008 at 9:50 PM
Edited Mar 13, 2008 at 9:51 PM
The change in IControllerFactory was made after DI users complained "How dare you resolve the type for me! I want the string!"

You don't need a builder strategy, all the required logic is exposed in the MVC framework already. See my blog post here for a UnityControllerFactory implementation that works with preview 2.
Mar 13, 2008 at 10:28 PM
Definitely the way to go!

If one wants to implement custom controller lookup given the name, do you still consider a BuilderStrategy at TypeMapping time would be an interesting place to do so?
Mar 13, 2008 at 11:35 PM
Awesome, Chris!
Mar 13, 2008 at 11:45 PM

francois_tanguay wrote:
Definitely the way to go!

If one wants to implement custom controller lookup given the name, do you still consider a BuilderStrategy at TypeMapping time would be an interesting place to do so?


It's certainly a possibility, but in my opinion is most likely overkill. The mapping of string to controller type is typically static, and the decision is made at compile time. If you wanted to map based on the string, I'd lean towards registering the names in the container and implementing a different controller factory that got the name.

If you were doing something really sophisticated that made lots of runtime decisions, then the strategy would be the way to go. I just can't see a need to do that. ;-)

-Chris