Unity and MVC–Resolving types challenge
Imagine you have a situation like this:
2 MVC controllers (A and B) have a dependency on SvcA**. SvcA has a dependency on SvcB and a component implementing IX.SvcB has a dependency on something implementing IY.
Every time the system resolves CtrlA we want to provide Y1 and every time it resolves CtrlB, we want to provide Y2.
Something like this:
and
How would you configure Unity to solve this?
No “if() else” allowed. System supplies its own MVC’s ControllerFactory. (via ControllerBuilder.Current.SetControllerFactory) . Ideally, we want the ControllerFactory to have no knowledge of any of these specific components.
Some help: read Chris’s blog and Unity docs.
Comments
Anonymous
January 26, 2011
IF (ControllerFactory to have no knowledge) THEN Write custom extension that will have this "logic" (those IFs for different CTRLs) and will return desired type/instance in special cases, otherwise will allow normal resolution of dependencies. ELSE use child contaner per each such drawn group class Somewhere you you will have to place those if (or translate ifs into some kind of hierarchy (interface/object), but in this case if will exist inside of unity during resolution)Anonymous
January 26, 2011
I thought about subcontainers too, but the code seemed to complicated. It could work though, there's no single (correct) answer. Thanks for posting yours!Anonymous
January 28, 2011
The comment has been removedAnonymous
January 28, 2011
Thanks Nick! I've never tried Autofac, but heard great things about it.