zoukankan      html  css  js  c++  java
  • Autofac 学习笔记草书 1 初识

    接口ILog

    1     public interface ILog
    2     {
    3         void Create(string log, params object[] paramters);
    4     }

    实现类PrintLoger

     1     public class PrintLoger:ILog
     2     {
     3         protected TextWriter TW{get;set;}
     4 
     5         public PrintLoger(TextWriter tw)
     6         { 
     7             this.TW=tw;
     8         }
     9 
    10         public void Create(string log,params object[] paramters)
    11         {
    12             if(log==null)
    13                 return;
    14 
    15             string Log=string.Format(log,paramters);
    16             TW.WriteLine(Log);
    17         }
    18     }

    Program测试与理解

     1 class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             Program p = new Program();
     6             p.Test4();
     7 
     8             Console.Read();
     9         }
    10 
    11         public void Test1()
    12         {
    13             var builder = new ContainerBuilder();
    14 
    15             builder.RegisterInstance(Console.Out).As<TextWriter>().ExternallyOwned();//code 0
    16             //builder.RegisterInstance(Console.Out);---同使用此句是相同的效果
    17             //应该是将一个组件(实例)注册到一个服务上?如果没有指明as,那么将会是默认的服务上?
    18 
    19             using (var container = builder.Build())
    20             {
    21                 //请求构造该服务上的一个组件的一个实例 ,发现是out
    22                 container.Resolve<TextWriter>().WriteLine("hello console.out->textwriter");
    23             }
    24         }
    25 
    26         public void Test2()
    27         {
    28             var builder = new ContainerBuilder();
    29             builder.RegisterInstance(Console.Out).As<TextWriter>().ExternallyOwned();//code 0
    30 
    31             builder.RegisterType<PrintLoger>().As<ILog>();// code 1
    32 
    33             using (var container2 = builder.Build())
    34             {
    35                 //请求Ilog的服务,将会构造相应注册组件的一个实例
    36                 //由于code 1 中 将组件PrintLoger 注册到Ilog服务上,则将会尝试构造PrintLoger实例
    37                 //使用其构造函数,将会请求TextWriter服务,由于code0 将 TextWriter 注册到该 TextWriter服务上,并且指明了实例为Out
    38                 //则构造参数TextWriter 的实例为Console.Out,构造成功
    39                 container2.Resolve<ILog>().Create("this is log :{0}", "Ilog-printerloger");
    40 
    41                 //请求PrintLoger服务,在Containter中并未找到相应的组件,无法构造实例,将会报错
    42                 container2.Resolve<PrintLoger>().Create("this is log :{1}", "printerloger");
    43             }
    44         }
    45 
    46         public void Test3()
    47         {
    48             var builder = new ContainerBuilder();
    49             builder.RegisterInstance(Console.Out).As<TextWriter>().ExternallyOwned();//code 0
    50 
    51 
    52             builder.RegisterType<PrintLoger>().As<PrintLoger>();//code3
    53 
    54             using (var container3 = builder.Build())
    55             {
    56                 //请求PrintLoger服务,由于code3注册组件PrintLoger到服务PrintLoger上,构造实例  由于code0使用 参数Console.out ,实例构造成功
    57                 container3.Resolve<PrintLoger>().Create("this is log:{0}", "printerloger11");
    58             }
    59         }
    60 
    61         public void Test4()
    62         {
    63             var builder = new ContainerBuilder();
    64 
    65             PrintLoger ploger = new PrintLoger(Console.Out);
    66             //builder.RegisterInstance(ploger).As<PrintLoger>();
    67             builder.RegisterInstance(ploger);
    68 
    69             using (var container3 = builder.Build())
    70             {
    71                 //请求PrintLoger服务,由于code3注册组件PrintLoger到服务PrintLoger上,构造实例  由于code0使用 参数Console.out ,实例构造成功
    72                 container3.Resolve<PrintLoger>().Create("this is log:{0}", "printerloger44");
    73             }
    74         }
    75     }

    总结

    重要的是理解组件和服务。Register 主要是将组件注册到服务,做一个映射。一个组件通常是以类,而一个服务通常是一个接口,也可以是一个类。Resolver会向Container中请求服务,而Container会查找其中注册到该服务的组件,并构造该组件的实例,如果注册的时候声明了实例,则使用该实例,如果没有,则会使用其默认的构造函数。如果函数内有参数,同样的原理,Container会继续寻找其组件,构造实例,完成原有服务组件的实例化。

    初识总结,难免有错误不恰当之处,务必请来者慎思而观,以免误导。若有来者纠误指点,定当欣喜,谢之。

    后若观之有误,则将改之。

  • 相关阅读:
    Vue:Axios异步通信,生命周期
    关于Hibernate多对多关联关系的更新问题。
    Hibernate多对多关联关系
    使用JQuery做一组复选框的功能。
    如何用jstl的select标签做二级联动下拉列表框??
    Ajax回退刷新页面问题的解决办法
    如何遍历一个JSON对象的属性值???
    一个关于JSON的异常,获取List对象失败的。。。
    阅读HandlerInterceptor接口源码的理解
    阅读HandlerInterceptor接口源码的理解
  • 原文地址:https://www.cnblogs.com/linecheng/p/2965265.html
Copyright © 2011-2022 走看看