Autofac使用

创建用例类

创建接口

namespace MyIBLL
{
    public interface IUserBLL
    {
        void AddUser(string name, string pass);
    }
}

一个类

namespace MyBLL
{
    public class UserBLL:IUserBLL
    {
        public void AddUser(string name, string pass)
        {
            Console.WriteLine($"新增用户{name}");
        }
    }
}

基础

       static void Main(string[] args)
        {
            //1.1 基本用法
            //用于从组件注册中构建 IContainer。
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<UserBLL>().As<IUserBLL>();
            //IContainer:为一组组件创建、连接依赖关系和管理生命周期。
            //IContainer的大多数实例都是由 ContainerBuilder创建的。
            IContainer container = builder.Build();
            //Resolve:从上下文中检索服务。
            IUserBLL userBll = container.Resolve<IUserBLL>();
            userBll.AddUser("tang san san","123");

            Console.Read();
        }

AsImplementedInterfaces

这里也可以使用 AsImplementedInterfaces 把所有接口都实例化

      static void Main(string[] args)
        {
            //1.2 基本用法
            ContainerBuilder builder = new ContainerBuilder();
            //实现所有接口
             builder.RegisterType<UserBLL>().AsImplementedInterfaces();
            IContainer container = builder.Build();
            IUserBLL userBll = container.Resolve<IUserBLL>();
            userBll.AddUser("tang san san","123");

            Console.Read();
        }

Assembly

对程序集注册,如果有很多接口和类,可以如下:

            //1.3 基本用法
            ContainerBuilder builder = new ContainerBuilder();
            //对程序集注册
            Assembly asm = Assembly.Load("MyBLL");
            builder.RegisterAssemblyTypes(asm).AsImplementedInterfaces();
            IContainer container = builder.Build();
            IUserBLL userBll = container.Resolve<IUserBLL>();
            userBll.AddUser("tang san san", "123");

创建实例方法

InstancePerDependency

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

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

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.

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.

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.

SingleInstance

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

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

InstancePerHttpRequest

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

参考:

七七 Autofac 依赖注入框架 使用
官方中文文档:https://autofaccn.readthedocs.io/zh/latest/

原文地址:https://www.cnblogs.com/tangge/p/10759725.html