• Autofac 依赖注入框架


    Autofac是一款IOC框架,比较于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很轻量级性能上非常高。

    官方网站http://autofac.org/

    优点:依赖注入不是目的,它是一系列工具和手段,最终的目的是帮助我们开发出松散耦合(loose coupled)、可维护、可测试的代码和程序。这条原则的做法是大家熟知的面向接口,或者说是面向抽象编程。

    IOC理解:假如我们要干一个工程,需要10个人,传统方法就是项目开始,这10个人都必须在场,捆绑在一起;而IOC就是项目开始的时候,在这里报个名,项目进程中,需要谁的时候,再让谁过来,减轻了他们之间的耦合,可以把IOC理解为一个类的管理器,项目启动的时候,注册所有的类,然后再根据实际需要,分别实例化

    方法一:注册组件,Register Components

    var builder = new ContainerBuilder();

    // Register individual components
    builder.RegisterInstance(new TaskRepository()).As<ITaskRepository>();
    builder.RegisterType<TaskController>();
    builder.Register(c => new LogManager(DateTime.Now)).As<ILogger>();

    // Scan an assembly for components
    builder.RegisterAssemblyTypes(myAssembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();

    var container = builder.Build();

    方法二:表达的相关性Express Dependencies

    public class TaskController
    {
      private ITaskRepository _repository;
      private ILogger _logger;

      // Autofac will automatically find the registered
      // values and pass them in for you.
      public TaskController(ITaskRepository repository, ILogger logger)
      {
        this._repository = repository;
        this._logger = logger;
      }
    }

    注:可以自动实现ILogger 等的构造初始化,当一个类被调用AutoFac会自动扫描这个类的构造函数,如果发现构造函数的参数类型和AutoFac一致,他就会自动取出该对象,并将该对象注入到当前类中,这过程就叫注入。

    方法三:MVC | 依赖注入 AutoFac (构造函数注入)

    var builder = new ContainerBuilder();
    builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly()); //注册mvc容器的实现
    var container = builder.Build();  //Build()方法是表示:创建一个容器 
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); //注册MVC容器 

    创建实例方法:

    1、InstancePerDependency
    对每一个依赖或每一次调用创建一个新的唯一的实例。这也是默认的创建实例的方式。

    官方文档解释:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.)

    2、InstancePerLifetimeScope
    在一个生命周期域中,每一个依赖或调用创建一个单一的共享的实例,且每一个不同的生命周期域,实例是唯一的,不共享的。

    官方文档解释:Configure the component so that every dependent component or call to Resolve() within a single ILifetimeScope gets the same, shared instance. Dependent components in different lifetime scopes will get different instances.

    3、InstancePerMatchingLifetimeScope
    在一个做标识的生命周期域中,每一个依赖或调用创建一个单一的共享的实例。打了标识了的生命周期域中的子标识域中可以共享父级域中的实例。若在整个继承层次中没有找到打标识的生命周期域,则会抛出异常:DependencyResolutionException。

    官方文档解释:Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope tagged with any of the provided tags value gets the same, shared instance. Dependent components in lifetime scopes that are children of the tagged scope will share the parent's instance. If no appropriately tagged scope can be found in the hierarchy an DependencyResolutionException is thrown.

    4、InstancePerOwned
    在一个生命周期域中所拥有的实例创建的生命周期中,每一个依赖组件或调用Resolve()方法创建一个单一的共享的实例,并且子生命周期域共享父生命周期域中的实例。若在继承层级中没有发现合适的拥有子实例的生命周期域,则抛出异常:DependencyResolutionException。

    官方文档解释:

    Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope created by an owned instance gets the same, shared instance. Dependent components in lifetime scopes that are children of the owned instance scope will share the parent's instance. If no appropriate owned instance scope can be found in the hierarchy an DependencyResolutionException is thrown.

    5、SingleInstance
    每一次依赖组件或调用Resolve()方法都会得到一个相同的共享的实例。其实就是单例模式。

    官方文档解释:Configure the component so that every dependent component or call to Resolve() gets the same, shared instance.

    6、InstancePerHttpRequest
    在一次Http请求上下文中,共享一个组件实例。仅适用于asp.net mvc开发。

    参考链接:

    autofac 创建实例方法总结:http://www.cnblogs.com/manglu/p/4115128.html

    AutoFac使用方法总结:Part I:http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/

  • 相关阅读:
    EdgeX Foundry初体验(五)-- Web Console图形界面(v1.0.0)
    第十九节:SQLServer通过发布订阅实现主从同步(读写分离)详解
    第六节:Ocelot之自身负载、网关限流、缓存和熔断机制
    第十九节:SQLServer通过发布订阅实现主从同步(读写分离)详解
    第十七节:分区、分表、分库以及基于EFCore实现读写分离
    第六节:IdentityServer4设备流授权模式和扫码登录(应用于IOT)
    第五节:IdentityServer4的Pkce机制、令牌刷新机制、混合授权模式
    第十一节:IdentityServer4授权码模式介绍和代码实操演练
    第十二节:Ocelot集成IDS4认证授权-微服务主体架构完成
    第十节:IdentityServer4隐式模式介绍和代码实操演练
  • 原文地址:https://www.cnblogs.com/honzhez/p/6344437.html
Copyright © 2020-2023  润新知