从Global.asax文件开始逐层分析Nop的架构。

Application_Start()方法作为mvc启动的第一个方法。

1,首先初始化一个引擎上下文,如下面的代码: EngineContext.Initialize(false);

引擎实现了IEngine接口,该接口定义如下:

public interface IEngine
    {
        ContainerManager ContainerManager { get; }

void Initialize(NopConfig config);

T Resolve<T>() where T : class;

object Resolve(Type type);

T[] ResolveAll<T>();
    }

2,在 EngineContext.Initialize(false)方法中具体做了如下工作:

首先是Singleton<IEngine>.Instance == null判断,Singleton<IEngine>.Instance是一个泛型单例模式,定义如下:Singleton<T> : Singleton,在singleton中定义了一个IDictionary<Type, object>集合,每次为instance赋值的时候,都会保存到这个集合中,从而缓存到整个应用程序中。

3,此行代码正是对实例赋值: Singleton<IEngine>.Instance = CreateEngineInstance(config)

接下来分析 CreateEngineInstance(config)是如何获取到引擎的实例的:

(1)参数config是NopConfig的一个实例,是通过读取web.config中节点NopConfig的信息,比较好理解。

(2)在配置文件中EngineType这个值是“”,所以就实例化一个默认的引擎: NopEngine。

(3)在实例化 NopEngine引擎时,调用了public NopEngine(EventBroker broker, ContainerConfigurer configurer)构造函数。

参数类型 broker=EventBroker.Instance; 一个http请求过程中的事件注册类,针对的事件主要是http请求过程中事件。

configurer=new ContainerConfigurer(); 实例化一个配置服务NOP使用控制反转容器

(4)EventBroker.Instance参数分析:通过该方式:Singleton<EventBroker>.Instance获取一个EventBroker实例。

4,通过构造以上三个参数,程序开始执行  InitializeContainer(configurer, broker, config),此过程是依赖注入,利用Autofac第三方类库。

代码分析:

(1) var builder = new ContainerBuilder();  创建一个依赖注入的容器构造器,所有的注入全是由它来完成。

(2)   _containerManager = new ContainerManager(builder.Build());  builder.Build() autofac来创建一个容器,并将该容器传递到nop自定义的容器管理类的构 造 函数中。ContainerManager 管理着注入方式的各种情况。

(3)接下来调用 configurer.Configure(this, _containerManager, broker, config);这个方法是配置依赖注入核心,在该方法中把应用程序的所有需要注入的分批注入。

A:注入了几个全局的配置,如下代码,

containerManager.AddComponentInstance<NopConfig>(configuration, "nop.configuration");
            containerManager.AddComponentInstance<IEngine>(engine, "nop.engine");
            containerManager.AddComponentInstance<ContainerConfigurer>(this, "nop.containerConfigurer");

来具体分析   containerManager.AddComponentInstance<IEngine>(engine, "nop.engine");者行代码主要做了什么工作。

调用方法的签名AddComponentInstance<TService>(object instance, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)

参数说明:instance: 实例名,也就是需要注入的实例,是一个object类型,也就意味着可以传入一切类型。

key:注入的键值名称,

lifeStyle:实例在容器中的生命周期,此参数配置为了单例,意味着在整个应用程序的生命周期中只有一个该实例。

接下来调用 UpdateContainer(x =>
            {
                var registration = x.RegisterInstance(instance).Keyed(key, service).As(service).PerLifeStyle(lifeStyle);
            });    UpdateContainer方法传递一个Action<ContainerBuilder>的委托。

x.RegisterInstance(instance).Keyed(key, service).As(service).PerLifeStyle(lifeStyle);这行代码是真正注入的过程。

注册完之后要更新一下容器,如下面代码:

builder.Update(_container);

B: 注册一个  containerManager.AddComponent<ITypeFinder, WebAppTypeFinder>("nop.typeFinder");WebAppTypeFinder类的作用是通过程序集反射出我们想要注入的内容。   该方法会调用 AddComponent(typeof(TService), typeof(TImplementation), key, lifeStyle);

然后调用

UpdateContainer(x =>
              {
                 var serviceTypes = new List<Type> { service };    //把接口放到一个list<type>的集合中。

if (service.IsGenericType)   //如果是泛型接口,进行下面的操作。
                 {
                    var temp = x.RegisterGeneric(implementation).As(
                        serviceTypes.ToArray()).PerLifeStyle(lifeStyle);
                    if (!string.IsNullOrEmpty(key))
                    {
                        temp.Keyed(key, service);
                    }
                 }
                 else   //不是泛型接口,进行下面的操作。
                 {
                    var temp = x.RegisterType(implementation).As(
                        serviceTypes.ToArray()).PerLifeStyle(lifeStyle);
                    if (!string.IsNullOrEmpty(key))
                    {
                        temp.Keyed(key, service);  //key值和list<type>相关联。
                    }
                 }
            });

C: 接下来我们就开始用我们刚刚注入到容器中的类。一下代码是调用的方法:

var typeFinder = containerManager.Resolve<ITypeFinder>();

代码分析:Resolve

public T Resolve<T>(string key = "") where T : class
          {
             if (string.IsNullOrEmpty(key))  //key值为空的情况下。会调用下面的方法。
             {
                return Scope().Resolve<T>();
             }
             return Scope().ResolveKeyed<T>(key);
         }

D:分析Scope().Resolve<T>() 方法是如何从容器中得到的实例。

public ILifetimeScope Scope()      //获取一个容器生命周期范围。
        {
            try
            {
                return AutofacRequestLifetimeHttpModule.GetLifetimeScope(Container, null);    //
            }
            catch
            {
                return Container;
            }
        }

方法AutofacRequestLifetimeHttpModule.GetLifetimeScope(Container, null)如下:

public static ILifetimeScope GetLifetimeScope(ILifetimeScope container, Action<ContainerBuilder> configurationAction)
        {
            //little hack here to get dependencies when HttpContext is not available
            if (HttpContext.Current != null)
            {

return LifetimeScope ?? (LifetimeScope = InitializeLifetimeScope(configurationAction, container));
            }
            else
            {
                //throw new InvalidOperationException("HttpContextNotAvailable");
                return InitializeLifetimeScope(configurationAction, container);
            }
        }

最后程序返回一个ILifetimeScope接口的实例。 接口继承关系:ILifetimeScope : IComponentContext

调用该接口的Resolve<T>()方法返回真正的对象。

方法实现代码:IComponentContext的扩展方法。

public static TService Resolve<TService>(this IComponentContext context)
        {
            return Resolve<TService>(context, NoParameters);
        }

至此实例从容器中获取到。

最新文章

  1. 物联网框架SuperIO 2.2.9和ServerSuperIO 2.1同时更新,更适用于类似西门子s7-200发送多次数据,才能读取数据的情况
  2. 聊聊JVM的年轻代
  3. mount: /dev/sdb1 already mounted or /mnt/hdb busy 导致NameNode无法启动
  4. IntelliJ IDEA常用设置及快捷键
  5. MVC客户端验证配置
  6. lucene 过滤结果
  7. linux perm
  8. UWP--页面传值
  9. async/await的多线程问题
  10. bug终结者 团队作业第二周
  11. Redis Sentinel集群双机房容灾实施步骤
  12. 理论篇-MySQL知识汇总
  13. LVS负载均衡群集(NAT)
  14. Java 接口 Cloneable
  15. C语言 &#183; 8皇后问题
  16. .NET Core中使用Docker
  17. 20155205 《Java程序设计》实验四 Android程序设计
  18. libffi-dev : 依赖: libffi6 (= 3.2.1-4) 但是 3.2.1-4kord 正要被安装
  19. &#39;curl&#39; is not recognized as an internal or external command
  20. python-Unix套接字

热门文章

  1. Linux学习方法
  2. appserv升级php
  3. java端口扫描(原创)
  4. OpenMP对于嵌套循环应该添加多少个parallel for 分类: OpenMP C/C++ Linux 2015-04-27 14:48 53人阅读 评论(0) 收藏
  5. 10-Java 网络通信
  6. 每日学习心得:$.extend()方法和(function($){...})(jQuery)详解
  7. 初学RabbitMQ
  8. 黄聪:PHP7.0中htmlspecialchars出错解决方案(wordpress)
  9. 切服务器时请注意robots.txt文件
  10. firefox与IE对js和CSS的区别(转http://log-cd.javaeye.com/blog/548665)