Asp.net MVC RouteBase和IOC [英] Asp.net MVC RouteBase and IoC

查看:111
本文介绍了Asp.net MVC RouteBase和IOC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过继承RouteBase创建自定义路线。我在那里有一种依赖,我想国际奥委会要连接。该方法GetRouteData只是需要HttpContext的,但我想在我的工作单位增加以及....莫名其妙。

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.

我使用StructureMap,但你会怎么做这与任何IoC框架信息将是有益的。

I am using StructureMap, but info on how you would do this with any IoC framework would be helpful.

推荐答案

嗯,这里是我们的解决方案。可以省略很多小细节,但总体思路是在这里。这个答案可能是一种offtop到原来的问题,但它描述的问题,一般的解决方案。

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.

我会尽力解释,负责由用户在运行时创建,因此不能有自己的控制器/动作纯自定义HTML的页面的部分。因此,路线应该是某种方式在运行时建立或为包罗万象定制IRouteConstraint。

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.


  • 我们有一些数据和有关存储在DB我们的网页一些元数据;

  • 我们不希望产生(假设)整个万元为所有现有的网页预先的路线(即在应用程序启动),因为东西可以应用在改变,我们不想推的变化对全球应对RouteCollection;

所以我们这样来做:

是的,专用控制器,负责我们所有的内容页面。还有就是这是唯一的行动显示(INT ID)(其实我们有一个特殊的视图模型的参数,但我用了一个内部编号为简单。

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 an int id for simplicity.

及其所有数据页由的ID 显示()方法内解决。该方法本身返回任何的ViewResult NotFoundResult PageViewModel 强类型) $ C>的情况下,如果找不到网页。

The page with all its data is resolved by ID inside that Display() method. The method itself returns either ViewResult (strongly typed after PageViewModel) or NotFoundResult in case when page is not found.

我们必须从某个地方定义是否实际请求的URL用户指的是我们自定义的页面。为此,我们有一个特殊的 IsPageConstraint 实现 IRouteConstraint 接口。在我们的约束匹配()方法,我们只需拨打我们的 PageRepository 来检查是否有匹配的页面我们请求的URL。我们有我们的PageRepository由StructureMap注入。如果我们发现页上,然后,我们添加ID参数(与数值)到的RouteData字典,它是自动绑定到 PageController.Display(INT ID) DefaultModelBinder

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 implements IRouteConstraint interface. In the Match() method of our constraint we just call our PageRepository 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 to PageController.Display(int id) by DefaultModelBinder.

但是,我们需要的RouteData参数来检查。我们在哪里买的?这里谈到...

But we need a RouteData parameter to check. Where we get that? Here comes...

重要提示:这条路线是在路由映射列表尽头定义,因为它是很一般,不具体。我们首先要检查我们的明确定义的路线,然后检查是否有(即容易改变,如果需要)。

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:

routes.MapRoute("ContentPages", 
                "{*pagePath}", 
                new { controller = "Page", action = "Display" }
                new { pagePath = new DependencyRouteConstraint<IsPageConstraint>() });

住手!那是什么 DependencyRouteConstraint 的事情出现在映射?好吧,那是什么做的伎俩。

Stop! What is that DependencyRouteConstraint thing appeared in mapping? Well, thats what does the trick.

这只不过是另一种普通的实施 IRouteConstraint 的这需要在真实 IRouteConstraint (IsPageConstraint)并解决它称为(给定的 TConstraint ),只有当匹配()方法。它使用依赖注入所以我们的 IsPageConstraint 实例已全部实际依赖注入!

This is just another generic implementation of IRouteConstraint which takes the "real" IRouteConstraint (IsPageConstraint) and resolves it (the given TConstraint) only when Match() method called. It uses dependency injection so our IsPageConstraint instance has all actual dependencies injected!

我们的 DependencyRouteConstraint 然后只需调用 dependentConstraint.Match()提供的所有参数从而也就委托实际的匹配向真正IRouteConstraint

Our DependencyRouteConstraint then just calls the dependentConstraint.Match() providing all the parameters thus just delegating actual "matching" to the "real" IRouteConstraint.

注:这个类实际上对ServiceLocator的依赖

Note: this class actually has the dependency on ServiceLocator.

这样的话,我们有:


  • 我们的路线清晰干净;

  • 是对服务定位为 DependencyRouteConstraint 的依赖唯一的类;

  • 任何自定义 IRouteConstraint 使用依赖注入需要的时候;

  • ???

  • 的利润!

  • Our Route clear and clean;
  • The only class that has a dependency on Service Locator is DependencyRouteConstraint;
  • Any custom IRouteConstraint uses dependency injection whenever needed;
  • ???
  • PROFIT!

希望这有助于。

这篇关于Asp.net MVC RouteBase和IOC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆