zoukankan      html  css  js  c++  java
  • 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 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.

  • 相关阅读:
    Spring Cloud 接口契约测试
    看我玩弄千万日志于股掌
    从哲学层面浅谈计算机学习方法论
    一切互联网优势都是效率优势,一切竞争最终都是效率之争
    Arduino--蜂鸣器
    Arduino--光感应模块--模拟输入
    Arduino---按钮
    Arduino--PWM引脚
    Arduino专用绘图软件Fritzing
    点亮LED灯
  • 原文地址:https://www.cnblogs.com/chucklu/p/10300239.html
Copyright © 2011-2022 走看看