应用程序引导程序
我正在阅读关于依赖注入的 StructureMap ,首先有两个部分来初始化映射,接口具体的类类型,另一种只是实例化(请求一个实例)。
第一部分需要配置和设置,提到这是在引导程序中完成的。
引导捆扎机的最佳做法是什么?具有静态构造函数的静态类?在IIS中怎么样?
另外,我如何配置结构图,以便在不重新启动应用程序的情况下,我可以更改依赖项?这可能吗?如何?
I am reading StructureMap about dependency injection, well there are two parts first to initialize the mapping, interface to concrete class type, and another one is just instantiation (asking for an instance).
First part requires configuration, setup which is mentioned to be done at a boot strapper.
what s the best practice for a boot strapper? static class with static constructors? how about in IIS?
Also, how can i configure Structure Map so that without restarting the application, I can change the dependencies? is that possible? how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
配置是在 Composition Root 中完成的。 IoC 容器对 ASP.NET WebForms 的支持非常糟糕。这些页面是由 IIS 创建的。您在这里唯一能做的就是在创建页面后注入属性。
如果您想为网站进行 DI,那么您应该使用 MVC 3 而不是 WebForms。在这种情况下,有一个集成包 Structuremap.MVC3 可以为您进行引导。你可以在nuget上找到它。 https://github.com/webadvanced/Structuremap-MVC3
The configuration is done in the Composition Root. IoC container support for ASP.NET WebForms is very bad. The pages are created by IIS. The only thing you can do here is to inject properties after the page is created.
If you want to do DI for Websites then you should use MVC 3 instead of WebForms. In this case there is an integration package Structuremap.MVC3 that does the bootstrapping for you. You can find it on nuget. https://github.com/webadvanced/Structuremap-MVC3
将静态类与静态构造函数一起使用并没有多大帮助,因为直到运行代码实际使用该类时才会调用静态构造函数。因此,最好的选择是在程序的
main()
方法中引导 DI。在 IIS 或类似环境中,加载或“启动”应用程序/加载项/组件时通常会触发一些事件。在 ASP.NET(即 IIS 中)中,
global.asax.cs
文件中的全局应用程序事件可用于此目的。Using a static class with a static constructor doesn't help much, because the static constructor won't get called until the class is actually used by the running code. Hence your best option is to bootstrap the DI in the program's
main()
method.In IIS or similar environment there are usually events that get fired when the application / add-in / component is loaded or 'started'. In ASP.NET (that is in IIS) the global application events in the
global.asax.cs
file serve this purpose.