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 to TodayWriter so starts creating a TodayWriter手动resolve
  • Autofac sees that the TodayWriter needs an IOutput in its constructor.
  • Autofac sees that IOutput maps to ConsoleOutput so creates a new ConsoleOutput instance.  自动resolve(构造函数注入)
  • Autofac uses the new ConsoleOutput instance to finish constructing the TodayWriter.
  • 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.

最新文章

  1. identity与ASP.NET 模拟
  2. OD19
  3. java.lang.NoClassDefFoundError:TagSupport
  4. 微信端应用 ionic实现texarea 自适应高度
  5. 第一个Struts1步骤
  6. Lambda中的一些方法的总结
  7. linux 下apache安装、启动和配置
  8. Elegant Box主题wpdb::prepare() 报错[已解决]
  9. Docker 打包 部署
  10. 超时导致的Galera节点加入集群失败
  11. [Python设计模式] 第24章 加薪审批——职责链模式
  12. maven 常用 Archetypes
  13. AtCoder Beginner Contest 045 B - 3人でカードゲームイージー / Card Game for Three (ABC Edit)
  14. 二、spring boot 1.5.4 异常控制
  15. hive常用参数配置设置
  16. eclipse 生成webservice 客户端
  17. python multiprocessing 使用
  18. Pygame - Python游戏编程入门(0) 转
  19. Codeforces Round #355 (Div. 2) C. Vanya and Label 水题
  20. CentOS7.x安装时的分区方案

热门文章

  1. hdu4045(递推)
  2. git GUI 入门
  3. iOS 7.1 UITapGestureRecognizer 不好用的解决办法
  4. Java获取任意时间、时间字符串
  5. POJ 3150 Cellular Automaton(矩阵快速幂)
  6. Access导入Sql 2008 R2 错误 0xc020801c
  7. jsp tutorial
  8. Python元组组成的列表转化为字典
  9. mysql乱码修改character_set_server
  10. 阿里云上Docker Compose部署wordpress