Autofac Getting Started(默认的构造函数注入)
https://autofaccn.readthedocs.io/en/latest/getting-started/index.html
The basic pattern for integrating Autofac into your application is:
- Structure your app with inversion of control (IoC) in mind.
- Add Autofac references.
- At application startup…
- Create a ContainerBuilder.
- Register components.
- Build the container and store it for later use.
- During application execution…
- Create a lifetime scope from the container.
- Use the lifetime scope to resolve instances of the components.
This getting started guide walks you through these steps for a simple console application. Once you have the basics down, you can check out the rest of the wiki for more advanced usage and integration information for WCF, ASP.NET, and other application types.
class Program { private static IContainer Container { get; set; } static void Main(string[] args) { var builder = new ContainerBuilder(); builder.RegisterType<ConsoleOutput>().As<IOutput>(); builder.RegisterType<TodayWriter>().As<IDateWriter>(); Container = builder.Build(); // The WriteDate method is where we'll make use // of our dependency injection. We'll define that // in a bit. WriteDate(); Console.ReadLine(); } public static void WriteDate() { // Create the scope, resolve your IDateWriter, // use it, then dispose of the scope. using (var scope = Container.BeginLifetimeScope()) { var writer = scope.Resolve<IDateWriter>(); writer.WriteDate(); } } }
- The “WriteDate” method asks Autofac for an
IDateWriter
. - Autofac sees that
IDateWriter
maps toTodayWriter
so starts creating aTodayWriter
. 手动resolve - Autofac sees that the
TodayWriter
needs anIOutput
in its constructor. - Autofac sees that
IOutput
maps toConsoleOutput
so creates a newConsoleOutput
instance. 自动resolve(构造函数注入) - Autofac uses the new
ConsoleOutput
instance to finish constructing theTodayWriter
. - Autofac returns the fully-constructed
TodayWriter
for “WriteDate” to consume.
Note: generally speaking, service location is largely considered an anti-pattern (see article). That is, manually creating scopes everywhere and sprinkling use of the container through your code is not necessarily the best way to go. Using the Autofac integration libraries you usually won’t have to do what we did in the sample app above. Instead, things get resolved from a central, “top level” location in the application and manual resolution is rare. Of course, how you design your app is up to you.
相关文章
- spring学习笔记(4)依赖注入详解
- 05-spring-bean注入
- 重新整理 .net core 实践篇————依赖注入应用之生命法则[三]
- 重新整理.net core 计1400篇[十] (.net core 中的依赖注入的服务的生命周期)
- 重新整理.net core 计1400篇[九] (.net core 中的依赖注入的服务注入)
- Nginx使用naxsi防xss、防注入攻击配置
- 【原创】MySQL Proxy - query注入动作中的脚本序列
- 记一次实战MSSQL注入绕过WAF
- ASP.NET Core中的依赖注入(1):控制反转(IoC)
- Angular Service依赖注入的一个具体例子
- Angular 依赖注入的学习笔记
- 【Android 逆向】Android 进程注入工具开发 ( 远程进程 注入动态库 文件操作 | Android 进程读取文件所需的权限 | fopen 打开文件标志位 | 验证文件权限 )
- SQL注入绕过技巧总结
- SQL注入关联分析
- 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)
- 1.3 DVWA亲测sql注入漏洞
- 安全测试===Mysql 注入技巧学习 MySQL注入技巧(2)
- 如何保护你的账户和财产不被Cookie劫持和HTML注入攻击?