Autofac

注册组件

using Autofac;
using System;

namespace ConsoleApp
{
    class Program
    {
        //建立容器
        private static IContainer Container { get; set; }
        static void Main(string[] args)
        {
            //建立映射表
            var builder = new ContainerBuilder();

            //建立自己与自己的映射
            //builder.RegisterType<One>();

            //建立接口与类的映射
            //builder.RegisterType<One>().As<IOne>();
            //同时建立自身及接口的映射
            //builder.RegisterType<One>().AsSelf().As<IOne>();
            //一个类可以由多个接口来绑定
            //builder.RegisterType<One>().AsSelf().As<IOne>();

            //当同一个接口绑定多个类时,以最后一个类为准
            //builder.RegisterType<One>().As<IOne>();
            //builder.RegisterType<One1>().As<IOne>();

            //建立映射关系,并制定构造函数
            //builder.RegisterType<One>().As<IOne>().UsingConstructor(typeof(ITwo));

            //提前建立好一个实体类(自己生成),Autofac只映射,不创建
            //One one = new One();    //用IOne接受亦可
            //builder.RegisterInstance(one).As<IOne>();

            //决定参数
            //builder.Register(c => new One(c.Resolve<ITwo>(), c.Resolve<IThree>())).As<IOne>();

            //传递复杂类型
            //builder.Register(c => new Two(DateTime.Now)).As<ITwo>();

            //builder.RegisterType<Two>().As<ITwo>();
            //builder.RegisterType<Three>().As<IThree>();
            //builder.RegisterType<Four>().As<IFour>();

            //可以根据运行时条件来选择具体执行哪一个
            //builder.Register<IThree>((c,p)=>
            //{
            //    //其他逻辑
            //    if (p.Named<int>("num") != 0)
            //    {
            //        return new Three(new Four());
            //    }
            //    return new Three1(new Four());
            //});

            //将表放入容器中使其生效
            Container = builder.Build();
            //var a = Container.Resolve<One>();
            //Fun(a);

            //动态决定生成的实例
            //Fun1(Container.Resolve<IThree>(new NamedParameter("num", 0)));

            Console.WriteLine("sss");
            Console.ReadLine();
        }

        public static void Fun(IOne one)
        {
            one.Fun();
        }
        public static void Fun1(IThree three)
        {
            Console.WriteLine(three);
        }

    }

    interface IOne
    {
        void Fun();
    }

    class One : IOne
    {
        public One()
        {
            Console.WriteLine("One");
        }

        public One(ITwo two)
        {
            Console.WriteLine("One Two");
        }

        public One(ITwo two,IThree three)
        {
            Console.WriteLine("One Two three");
        }

        public void Fun()
        {
            Console.WriteLine("One Fun");
        }
    }

    class One1 : IOne
    {
        public void Fun()
        {
            throw new NotImplementedException();
        }
    }

    interface ITwo
    {
        void Fun();
    }

    class Two : ITwo
    {
        public Two()
        {
            Console.WriteLine("Two");
        }

        public Two(DateTime dateTime)
        {
            Console.WriteLine(dateTime);
        }

        public void Fun()
        {
            Console.WriteLine("Two Fun");
        }
    }

    interface IThree
    {

    }

    class Three : IThree
    {
        public Three(IFour four)
        {
            Console.WriteLine("Three");
        }
    }

    class Three1 : IThree
    {
        public Three1(IFour four)
        {
            Console.WriteLine("Three1");
        }
    }

    public interface IFour
    {

    }
    public class Four : IFour
    {

    }

}

程序集扫描

using Autofac;
using ClassLibrary1;
using System;
using System.Reflection;


namespace ConsoleApp
{
    class Program
    {
        //建立容器
        private static IContainer Container { get; set; }
        static void Main(string[] args)
        {
            //建立映射表
            var builder = new ContainerBuilder();

            //获取某一个类所在的程序集
            //var assembly = typeof(Ten).Assembly;
            //获取当前类类所在的程序集
            //var assembly = Assembly.GetExecutingAssembly();
            //Assembly assembly = Assembly.Load("ClassLibrary1");

            //注册程序集中的类
            //builder.RegisterAssemblyTypes(assembly)
            //       //有条件的注册程序集中类,不使用规则,则默认注册所有的类
            //       .Where(t => t.Name.EndsWith("Service"))
            //       //从扫描的类型中排除
            //       .Except<LoginService>()
            //       .AsImplementedInterfaces();

            //可以运用多种规则
            //builder.RegisterAssemblyTypes(assembly)
            //       //有条件的注册程序集中类
            //       .Where(t => t.Name.EndsWith("Ten"))
            //       .AsImplementedInterfaces();

            //表示类最终以接口来映射类
            //builder.RegisterType<One>().AsImplementedInterfaces();

            //注册一个Module所在的程序集,会将这个程序集中所有Module都注册
            var assembly = typeof(AModule).Assembly;
            //builder.RegisterAssemblyModules(assembly);
            //注册一个Module
            //builder.RegisterAssemblyModules<AModule>(assembly);
            //注册一个Module
            builder.RegisterAssemblyModules(typeof(AModule),assembly);

            //注册一个Module
            //builder.RegisterModule(new AModule());
            //builder.RegisterModule(new BModule());


            //将表放入容器中使其生效
            Container = builder.Build();
            var a = Container.Resolve<ITen>();
            Fun1(a);
            var b = Container.Resolve<ILoginService>();
            var c = Container.Resolve<ILogoutService>();
            Fun(b,c);


            Console.WriteLine("sss");
            Console.ReadLine();
        }

        static void Fun(ILoginService login, ILogoutService logout)
        {
            
        }

        static void Fun1(ITen ten)
        {

        }


    }

    public class AModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var assembly = typeof(Ten).Assembly;
            builder.RegisterAssemblyTypes(assembly)
                   .Where(t => t.Name.EndsWith("Service"))
                   .AsImplementedInterfaces();
        }
    }

    public class BModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var assembly = typeof(Ten).Assembly;
            builder.RegisterAssemblyTypes(assembly)
                   .Where(t => t.Name.EndsWith("Ten"))
                   .AsImplementedInterfaces();
        }
    }



}
using System;

namespace ClassLibrary1
{
    public interface ITen
    {
        void Fun();
    }
    public class Ten : ITen
    {
        public void Fun()
        {
            Console.WriteLine("Ten");
        }
    }

    public interface ILoginService { }

    public class LoginService : ILoginService { }

    public interface ILogoutService
    {

    }

    public class LogoutService : ILogoutService
    {

    }
}

传参

    class Program
    {
        //建立容器
        private static IContainer Container { get; set; }
        static void Main(string[] args)
        {
            //建立映射表
            var builder = new ContainerBuilder();

            builder.RegisterType<LoginService>().AsImplementedInterfaces();
            //注册时传参
            //builder.Register(c => new LoginService("zhang1")).AsImplementedInterfaces();
            //builder.RegisterType<LoginService>()
            //    .AsImplementedInterfaces()
            //    .WithParameter("name", "zhang2");
            //builder.RegisterType<LoginService>()
            //    .AsImplementedInterfaces()
            //    .WithParameter(new TypedParameter(typeof(string), "zhang3"));
            //builder.RegisterType<LoginService>()
            //    .AsImplementedInterfaces()
            //    .WithParameter(
            //    new ResolvedParameter(
            //    (pi, ctx) => pi.ParameterType == typeof(string) && pi.Name == "name",
            //    (pi, ctx) => "zhang4"));

            //将表放入容器中使其生效
            Container = builder.Build();

            //解析时传参
            //var b = Container.Resolve<ILoginService>(new NamedParameter("name","zhang5"));
            //var b = Container.Resolve<ILoginService>(new TypedParameter(typeof(string), "zhang6"));
            //var b = Container.Resolve<ILoginService>(
            //    new TypedParameter(typeof(string), "zhang6"),
            //    new ResolvedParameter(
            //    (pi, ctx) => pi.ParameterType == typeof(string) && pi.Name == "name",
            //    (pi, ctx) => "zhang7"));
            //Fun(b);


            Console.WriteLine("sss");
            Console.ReadLine();
        }

        static void Fun(ILoginService login)
        {
            
        }
    }
    public interface ILoginService { }

    public class LoginService : ILoginService 
    {
        public LoginService(string name)
        {
            Console.WriteLine(name);
        }
    }

关系类型

Transiten:InstancePerDependency 默认 每个依赖一个实例

Singleton:SingleInstance 单一实例

Scope:InstancePerLifetimeScope 每个生命周期作用域一个实例

ASP.NET Core 使用的是每个生命周期一个实例而不是每个请求一个实例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值