Document

通过配置的方式Autofac 《第三篇》

一、基本配置

  1、通过配置的方式使用Autofac

复制代码
  <?xml version="1.0"?>
  <configuration>
    <configSections>
      <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
    </configSections>
    <autofac defaultAssembly="ConsoleApplication3">
      <components>
        <component type="ConsoleApplication3.Worker, ConsoleApplication3" service="ConsoleApplication3.IPerson" />
      </components>
    </autofac>
  </configuration>
复制代码

  2、通过RegisterModule方式使用配置文件中的信息

复制代码
    static void Main(string[] args)
    {
        ContainerBuilder builder = new ContainerBuilder();
        builder.RegisterType<AutoFacManager>();
        builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
        using (IContainer container = builder.Build())
        {
            AutoFacManager manager = container.Resolve<AutoFacManager>();
            manager.Say();
        } 
            
        Console.ReadKey();
    }
复制代码

  3、通过Register的方式

    builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
    builder.Register(c => new AutoFacManager(c.Resolve<IPerson>()));
posted @ 2017-07-13 15:01  从未被超越  阅读(291)  评论(0编辑  收藏  举报