简体   繁体   English

带有Autofac的ASP.NET MVC3引导程序

[英]ASP.NET MVC3 Bootstrapper with Autofac

I have some questions regarding a bootstrapper class. 我对引导程序类有一些疑问。 I am using ASP.NET MVC 3 . 我正在使用ASP.NET MVC 3

Is it still wise to use a bootstrapper in MVC3 to reduce the amount of code in the global.asax file? 在MVC3中使用引导程序来减少global.asax文件中的代码量仍然明智吗?

What exactly must be loaded in the boostrapper besides the registering of routes? 除了路由注册外,还必须在boostrapper中加载什么? If I want to load site-wide settings (loaded from a database), do I do that here? 如果要加载站点范围的设置(从数据库加载),是否在这里执行? If this is the case then do I need to inject these settings into each controller? 如果是这种情况,那么我需要将这些设置注入到每个控制器中吗? If it gotten in the bootstrapper, how are these values retained? 如果进入引导程序,这些值将如何保留?

I am looking for code/articles on how to use Autofac with my bootstrapper. 我正在寻找有关如何在Bootstrapper中使用Autofac的代码/文章。 I can only find for Ninject. 我只能找到Ninject。 Has anyone got some code or articles for me? 有人给我一些代码或文章吗?

I found these 2 good articles: 我找到了这两篇好文章:

  1. http://www.dominicpettifer.co.uk/Blog/42/put-an-ioc-powered-bootstrapper-in-your-asp-net-mvc-application http://www.dominicpettifer.co.uk/Blog/42/put-an-ioc-powered-bootstrapper-in-your-asp-net-mvc-application
  2. http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell.aspx http://weblogs.asp.net/rashid/archive/2009/02/17/use-bootstrapper-in-your-asp-net-mvc-application-and-reduce-code-smell.aspx

I am trying to accomplish something like: 我正在尝试完成类似的事情:

protected void Application_Start()
{
     Bootstrapper.Run();
}

Is it still wise to use a bootstrapper in MVC3 to reduce the amount of code in the global.asax file? 在MVC3中使用引导程序来减少global.asax文件中的代码量仍然明智吗?

I'm using let's say a bootstrapper (in fact various start up tasks executed in a particular order) in every web applciation. 我在每个Web应用程序中使用引导程序(实际上是以特定顺序执行的各种启动任务)进行引导。 I put in those tasks all the things I need to be setup: from routes and global filters, to setup the DI container, loading settings from the db (after setting up the DI container) and so on. 我将所有需要设置的东西放到这些任务中:从路由和全局过滤器,到设置DI容器,从db中加载设置(在设置DI容器之后),等等。

Basically everything that you could have put in global.asax.cs Application_Startnow resides as tasks in a Start Up directory, and each task is a simple class marked as a startup task (I'm using my own toolkit for that). 基本上,您可以在global.asax.cs Application_Startnow中放入的所有内容都作为任务驻留在“启动”目录中,并且每个任务都是标记为启动任务的简单类(我为此使用了自己的工具包)。 The benefits of a bootstrapper is the easy mantainance of the those tasks, it has no implicit connection with an IoC Container, however setting up the container is usually something that is executed when the app starts. 引导程序的好处是可以轻松维护这些任务,它与IoC容器没有隐式连接,但是设置容器通常是在应用程序启动时执行的。

If I want to load site-wide settings (loaded from a database), do I do that here 如果要加载站点范围的设置(从数据库加载),请在此处执行此操作

Only if the settings are static or required to configure the app. 仅当设置为静态或配置应用程序为必需时。 It pretty much depends on the type of settings and the type of app. 这在很大程度上取决于设置的类型和应用程序的类型。 There isn't a clear and definite answer. 没有一个明确的答案。

If this is the case then do I need to inject these settings into each controller 如果是这种情况,那么我需要将这些设置注入到每个控制器中吗

You don't inject settings in a controller, you inject dependecies and that's the IoC container's job. 您没有在控制器中注入设置,而是注入了依赖关系,这就是IoC容器的工作。

If it gotten in the bootstrapper, how are these values retained? 如果进入引导程序,这些值将如何保留?

The bootstrapper is used only once when app starts, to configure the app then it's out. 引导程序仅在应用程序启动时使用一次,以配置应用程序,然后将其退出。 It shouldn't retain values. 它不应该保留价值。 You may be thinking about settings but even if the bootstrapper loads them, those settings are stored in a different place, usually a cache. 您可能正在考虑设置,但是即使引导程序加载了这些设置,这些设置也会存储在其他位置,通常是缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM