I am creating a custom route by subclassing RouteBase. I have a dependency in there that I'd like to wire up with IoC. The method GetRouteData just takes HttpContext, but I want to add in my unit of work as well....somehow.
I am using StructureMap, but info on how you would do this with any IoC framework would be helpful.
Well, here is our solution. Many little details may be omitted but overall idea is here. This answer may be a kind of offtop to original question but it describes the general solution to the problem.
I'll try to explain the part that is responsible for plain custom HTML-pages that are created by users at runtime and therefore can't have their own Controller/Action. So the routes should be either somehow built at runtime or be "catch-all" with custom IRouteConstraint.
First of all, lets state some facts and requirements.
So we do it this way:
1. PageController
Yes, special controller that is responsible for all our content pages. And there is the only action that is
Display(int id)
(actually we have a special ViewModel as param but I used anint id
for simplicity.The page with all its data is resolved by ID inside that
Display()
method. The method itself returns eitherViewResult
(strongly typed afterPageViewModel
) orNotFoundResult
in case when page is not found.2. Custom IRouteConstraint
We have to somewhere define if the URL user actually requested refers to one of our custom pages. For this we have a special
IsPageConstraint
that implementsIRouteConstraint
interface. In theMatch()
method of our constraint we just call ourPageRepository
to check whether there is a page that match our requested URL. We have our PageRepository injected by StructureMap. If we find the page then we add that "id" parameter (with the value) to the RouteData dictionary and it is automatically bound toPageController.Display(int id)
byDefaultModelBinder
.But we need a RouteData parameter to check. Where we get that? Here comes...
3. Route mapping with "catch-all" parameter
Important note: this route is defined in the very end of route mappings list because it is very general, not specific. We check all our explicitly defined routes first and then check for a
Page
(that is easily changeable if needed).We simply map our route like this:
Stop! What is that
DependencyRouteConstraint
thing appeared in mapping? Well, thats what does the trick.4. DependencyRouteConstraint<TConstraint> class
This is just another generic implementation of
IRouteConstraint
which takes the "real"IRouteConstraint
(IsPageConstraint) and resolves it (the givenTConstraint
) only whenMatch()
method called. It uses dependency injection so ourIsPageConstraint
instance has all actual dependencies injected!Our
DependencyRouteConstraint
then just calls thedependentConstraint.Match()
providing all the parameters thus just delegating actual "matching" to the "real" IRouteConstraint.Note: this class actually has the dependency on ServiceLocator.
Summary
That way we have:
Route
clear and clean;DependencyRouteConstraint
;IRouteConstraint
uses dependency injection whenever needed;Hope this helps.
So, the problem is:
And that is why I don't see the children of RouteBase class as a good place for some DB work dependency. It makes everything closely coupled and non-scalable. It is actually impossible to perform Dependency Injection.
From now (I guess there is some kind of already working system) you actually have just one more or less viable option that is:
Route
.With CSL you have to just call inside
GetRouteData
:or with just StructureMap (without CSL facade):
and you're done. Quick and dirty. And the keyword is "dirty" actually :)
Sure, there is much more flexible solution but it needs a few architectural changes. If you provide more details on exactly what data you get in your routes I can try to explain how we solved our
Pages
routing problem (using DI and customIRouteConstraint
).